MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。

理解MyBatis

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。 MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。 MyBatis 可以使用简单的XML 或注解用于配置和原始映射,将接口和 Java 的 POJO( Plain Old Java Objects,普通的Java 对象)映射成数据库中的记录.

Mybatis的功能架构分为三层:

1)       API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。

2)       数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。

3)      基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。

在SpringBoot中集成Mybatis

<1>在Pom中添加依赖

添加Mybatis相关Jar包

  1. <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>1.3.</version>
  6. </dependency>

添加Mysql jdbc相关Jar包

  1. <properties>
  2. <mysql-connector>5.1.</mysql-connector>
  3. </properties>
  1. <!-- MySQL 连接驱动依赖 -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>${mysql-connector}</version>
  6. </dependency>

<2>为项目添加配置application.properties

  1. spring.application.name=push.messagepush01
  2. server.port=
  3.  
  4. spring.datasource.name = defaultDatasource4Phihome
  5.  
  6. spring.datasource.url=jdbc:mysql://localhost:/your_database?useUnicode=true&characterEncoding=utf8&useSSL=false
  7.  
  8. spring.datasource.username=root
  9. spring.datasource.password=
  10. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  11. spring.datasource.maximum-pool-size =
  12. spring.datasource.sql-script-encoding = UTF-
  13. spring.datasource.min-idle =
  14. spring.datasource.initial-size =
  15. spring.datasource.max-active =
  16. spring.datasource.auto-commit = true
  17.  
  18. spring.datasource.validation-query=SELECT
  19. spring.datasource.test-on-borrow=false
  20. spring.datasource.test-while-idle=true
  21. spring.datasource.time-between-eviction-runs-millis=

<3>编写程序代码

<3.1>编写entity:

  1. package com.phicomm.push.messagepush01.model.entity;
  2.  
  3. public class AppDaoModel {
  4.  
  5. private int app_id;
  6. private String registration_id;
  7. private String alias;
  8. private String tag;
  9. private String app_product_id;
  10. private String version;
  11. private String os_type;
  12. private long app_regdate=0L;
  13. private String pushcompany;
  14. private long app_lastpushdate=0L;
  15. private String app_wblist;
  16. private long create_time=0L;
  17. private long update_time=0L;
  18. public int getApp_id() {
  19. return app_id;
  20. }
  21. public void setApp_id(int app_id) {
  22. this.app_id = app_id;
  23. }
  24. public String getRegistration_id() {
  25. return registration_id;
  26. }
  27. public void setRegistration_id(String registration_id) {
  28. this.registration_id = registration_id;
  29. }
  30. public String getAlias() {
  31. return alias;
  32. }
  33. public void setAlias(String alias) {
  34. this.alias = alias;
  35. }
  36. public String getTag() {
  37. return tag;
  38. }
  39. public void setTag(String tag) {
  40. this.tag = tag;
  41. }
  42. public String getApp_product_id() {
  43. return app_product_id;
  44. }
  45. public void setApp_product_id(String app_product_id) {
  46. this.app_product_id = app_product_id;
  47. }
  48. public String getVersion() {
  49. return version;
  50. }
  51. public void setVersion(String version) {
  52. this.version = version;
  53. }
  54. public String getOs_type() {
  55. return os_type;
  56. }
  57. public void setOs_type(String os_type) {
  58. this.os_type = os_type;
  59. }
  60. public long getApp_regdate() {
  61. return app_regdate;
  62. }
  63. public void setApp_regdate(long app_regdate) {
  64. this.app_regdate = app_regdate;
  65. }
  66. public String getPushcompany() {
  67. return pushcompany;
  68. }
  69. public void setPushcompany(String pushcompany) {
  70. this.pushcompany = pushcompany;
  71. }
  72. public long getApp_lastpushdate() {
  73. return app_lastpushdate;
  74. }
  75. public void setApp_lastpushdate(long app_lastpushdate) {
  76. this.app_lastpushdate = app_lastpushdate;
  77. }
  78. public String getApp_wblist() {
  79. return app_wblist;
  80. }
  81. public void setApp_wblist(String app_wblist) {
  82. this.app_wblist = app_wblist;
  83. }
  84. public long getCreate_time() {
  85. return create_time;
  86. }
  87. public void setCreate_time(long create_time) {
  88. this.create_time = create_time;
  89. }
  90. public long getUpdate_time() {
  91. return update_time;
  92. }
  93. public void setUpdate_time(long update_time) {
  94. this.update_time = update_time;
  95. }
  96. public int getStatus() {
  97. return status;
  98. }
  99. public void setStatus(int status) {
  100. this.status = status;
  101. }
  102. private int status;
  103.  
  104. }

<3.2>编写Interface Service

  1. package com.phicomm.push.messagepush01.service;
  2.  
  3. import java.util.List;
  4.  
  5. import com.phicomm.push.messagepush01.model.entity.AppDaoModel;
  6.  
  7. public interface AppService {
  8.  
  9. public List<AppDaoModel> getAllAppInfo();
  10.  
  11. public List<AppDaoModel> selectAppInfo(int app_id);
  12.  
  13. public int addAppInfo(AppDaoModel appDaoModel);
  14.  
  15. public int updateAppInfo(AppDaoModel appDaoModel);
  16.  
  17. public int deleteAppInfo(int app_id);
  18. }

<3.3>编写Interface Mapper

  1. package com.phicomm.push.messagepush01.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.annotations.Delete;
  6. import org.apache.ibatis.annotations.Insert;
  7. import org.apache.ibatis.annotations.Param;
  8. import org.apache.ibatis.annotations.Select;
  9. import org.apache.ibatis.annotations.Update;
  10.  
  11. import com.phicomm.push.messagepush01.model.entity.AppDaoModel;
  12.  
  13. public interface AppMapper {
  14.  
  15. @Select("select * from phi_push_appinfo")
  16. public List<AppDaoModel> getAllAppInfo();
  17. @Select("select * from phi_push_appinfo where app_id=#{app_id}")
  18. public List<AppDaoModel> selectAppInfo(@Param("app_id") int app_id);
  19. @Insert("insert into phi_push_appinfo(registration_id,alias,tag) values(#{appinfo.registration_id},#{appinfo.alias},#{appinfo.tag})")
  20. public int addAppInfo(@Param("appinfo") AppDaoModel appinfo);
  21. @Update("update phi_push_appinfo set registration_id=#{appinfo.registration_id},alias=#{appinfo.alias},tag=#{appinfo.tag} where app_id=#{appinfo.app_id}")
  22. public int updateAppInfo(@Param("appinfo") AppDaoModel appinfo);
  23. @Delete("delete from phi_push_appinfo where app_id=#{app_id}")
  24. public int deleteAppInfo(@Param("app_id") int app_id);
  25. }

<3.4>编写Service的实现

  1. package com.phicomm.push.messagepush01.service.imp;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.annotation.Resource;
  6.  
  7. import org.springframework.stereotype.Service;
  8.  
  9. import com.phicomm.push.messagepush01.dao.AppMapper;
  10. import com.phicomm.push.messagepush01.model.entity.AppDaoModel;
  11. import com.phicomm.push.messagepush01.service.AppService;
  12.  
  13. @Service
  14. public class AppServiceImp implements AppService{
  15.  
  16. @Resource
  17. private AppMapper mapper;
  18. @Override
  19. public List<AppDaoModel> selectAppInfo(int app_id) {
  20. // TODO Auto-generated method stub
  21. return mapper.selectAppInfo(app_id);
  22. }
  23.  
  24. @Override
  25. public int addAppInfo(AppDaoModel appDaoModel) {
  26. // TODO Auto-generated method stub
  27. return mapper.addAppInfo(appDaoModel);
  28. }
  29.  
  30. @Override
  31. public int updateAppInfo(AppDaoModel appDaoModel) {
  32. // TODO Auto-generated method stub
  33. return mapper.updateAppInfo(appDaoModel);
  34. }
  35.  
  36. @Override
  37. public int deleteAppInfo(int app_id) {
  38. // TODO Auto-generated method stub
  39. return mapper.deleteAppInfo(app_id);
  40. }
  41.  
  42. @Override
  43. public List<AppDaoModel> getAllAppInfo() {
  44. // TODO Auto-generated method stub
  45. return mapper.getAllAppInfo();
  46. }
  47.  
  48. }

<3.5>编写Controller

  1. package com.phicomm.push.messagepush01.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.logging.log4j.LogManager;
  6. import org.apache.logging.log4j.Logger;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10.  
  11. import com.phicomm.push.messagepush01.model.entity.AppDaoModel;
  12. import com.phicomm.push.messagepush01.service.AppService;
  13.  
  14. @RestController
  15. @RequestMapping("/appinfo")
  16. public class AppInfoController {
  17.  
  18. private static Logger logger=LogManager.getLogger(AppInfoController.class);
  19. @Autowired
  20. private AppService appService;
  21.  
  22. @RequestMapping("/getall")
  23. public List<AppDaoModel> getAllAppInfo(){
  24. logger.info("get all app info!");
  25.  
  26. List<AppDaoModel> a=appService.getAllAppInfo();
  27. for(AppDaoModel x:a) {
  28. logger.debug("Alias is: "+x.getAlias());
  29. logger.debug("Tag is: "+x.getTag());
  30. }
  31. logger.info("select successed!");
  32. return a;
  33. }
  34. @RequestMapping("/get")
  35. public List<AppDaoModel> getAppInfo(int app_id){
  36.  
  37. return null;
  38. }
  39. @RequestMapping("/add")
  40. public String regAppInfo(AppDaoModel appDaoModel) {
  41.  
  42. return "";
  43. }
  44. @RequestMapping("/upd")
  45. public String updAppInfo(AppDaoModel appDaoModel) {
  46.  
  47. return "";
  48. }
  49. @RequestMapping("/del")
  50. public String delAppInfo(int app_id) {
  51.  
  52. return "";
  53. }
  54. }

注意项目中要单独写一个Main类用来加载Main方法

<3.6>编写入口类

  1. package com.phicomm.push.messagepush01;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.annotation.PropertySource;
  7. import org.springframework.context.annotation.PropertySources;
  8.  
  9. @SpringBootApplication(scanBasePackages= { "com.phicomm.push.messagepush01.**" })
  10. @PropertySources(@PropertySource(value="classpath:application.properties",ignoreResourceNotFound=true))
  11. @MapperScan("com.phicomm.push.messagepush01.dao.**")
  12. public class PhiPushMain {
  13.  
  14. public static void main(String[] args) {
  15. SpringApplication.run(PhiPushMain.class, args);
  16. }
  17. }

<4>测试连接数据库

首次创建的数据库会报错,提示连不上 Access denied for user 'root'@'localhost' (using password:YES)

因为用jdbc连接数据库的时候root都不太好使,可以参考网络上的调整root的权限,也可以直接新建一个用户专门用来进行远程连接

Mybatis入门配置的更多相关文章

  1. mybatis入门配置和调试

    欢迎转载http://www.cnblogs.com/jianshuai520/p/8669177.html大家一起努力,如果看的时候有图片半边遮挡起来的话,右键查看图片,就可以观看完整的图片,具体怎 ...

  2. mybatis入门--配置

    1.导入jar包 mybatis-x.x.x.jar 导入到lib目录下, 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中: <depen ...

  3. Mybatis入门配置及第一个Mybatis程序

    目的:使用mybatis来进行对数据库表的操作 第一步:引入jar包 我这里是创建的maven工程 第二步:创建数据表user 第三步:创建实体类 实体类放在包 com.xxx.pojo 下,包名可自 ...

  4. MyBatis入门程序(基于XML配置)

    创建一个简单的MyBatis入门程序,实现对学生信息的增删改查功能(基于XML配置) 一.新建一个Java工程,导入MyBatis核心jar包.日志相关的jar包以及连接Oracle数据库所需驱动包, ...

  5. Mybatis入门(四)配置别名(二)

    这一章我们练习一下Mybatis的别名,这大大的提高了我们的开发效率 类型别名(typeAliases) 类型别名是为 Java 类型设置一个短的名字. 它只和 XML 配置有关,作用在于用来减少类完 ...

  6. Mybatis入门(四)配置优化(一)

    这一章主要实验Mybatis的引入外部配置文件,属性(properties)这个属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素 ...

  7. MyBatis1:MyBatis入门

    MyBatis是什么 MyBatis是什么,MyBatis的jar包中有它的官方文档,文档是这么描述MyBatis的: MyBatis is a first class persistence fra ...

  8. MyBatis入门基础(一)

    一:对原生态JDBC问题的总结 新项目要使用mybatis作为持久层框架,由于本人之前一直使用的Hibernate,对mybatis的用法实在欠缺,最近几天计划把mybatis学习一哈,特将学习笔记记 ...

  9. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

随机推荐

  1. layDate/DatePicker日期时间空间

    真心不错,果断收藏了. 1.示例与效果 2.更多示例与皮肤 补充说明:My97DatePicker日期时间插件 的使用 1.示例与效果 2. 更多 常用的实例:WdatePicker下载 http:/ ...

  2. 『Spring.NET+NHibernate+泛型』框架搭建之DAO(三)★

    本节内容介绍Nhibernate所封装的数据库訪问层.只是我增加了泛型进行封装.大概思路:首先,我们有一个接口层,另一个相应的实现层.在接口层中我们先定义一个父接口,父接口中定义每个接口都可能会用到的 ...

  3. RabbitMQ用户角色及权限控制 -2

    1.RabbitMQ的用户角色分类: none.management.policymaker.monitoring.administrator none 不能访问 management plugin ...

  4. 【MongoDB】嵌套数组查询方案

    From:http://stackoverflow.com/questions/12629692/querying-an-array-of-arrays-in-mongodb 数据 db.multiA ...

  5. Python爬虫(六)

    源码: import requests import re from my_mysql import MysqlConnect # 获取问答信息 def get_contents(page,heade ...

  6. UNMET PEER DEPENDENCY @angular/common@2.3.1

    install of angular-cli results in unmeet peer dependencies. OSX 10.11.6node v6.9.1npm v3.10.8 [sudo] ...

  7. Cognos组织架构介绍

    Cognos只是一个工具,说到Cognos相信大部分人都知道BI(商业智能,Business Intelligence). Cognos也是属于SOA架构,面向服务的体系结构,是一个组件模型,它将应用 ...

  8. CxGrid如何实现导出Excel 功能

    ExportGrid4ToEXCEL  这个老的版本用的,新的版本引用 cxGridExportLink 这个单元 uses  Windows, Messages, SysUtils, Variant ...

  9. eclipse中的SVN文件还原到历史版本

    转载自:http://www.softown.cn/post/103.html 由于某些特殊原因,我们可能需要将SVN资源库中的某个文件回滚到以前的某个历史版本(准确地说,这不是"回滚&qu ...

  10. LAMP集群项目五 nfs存储的数据实时同步到backupserver

    tar fxzsersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ mv GNU-Linux-x86 sersync cp sers ...