SpringBoot整合Mybatis 以及 mybatis自动生成代码配置

1、springboot的配置文件application.yml

[html]  view plain  copy
  1. #端口  
  2. server:  
  3.   port: 8080  
  4.   
  5.   #模板页面  
  6.   #注释的部分是Thymeleaf默认的配置,如有其它需求可以自行更改  
  7.   spring:  
  8.     thymeleaf:  
  9.       cache: false  
  10.       prefix: classpath:/templates/  
  11.       suffix: .html  
  12.       mode: LEGACYHTML5  
  13. #      encoding: UTF-8  
  14. #      content-type: text/html  
  15.   
  16. ##数据源一  
  17. #spring:  
  18. #      datasource:  
  19. #          driverClass: com.mysql.jdbc.Driver  
  20. #          url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8  
  21. #          username: xuan  
  22. #          password: 123456  
  23. #数据源二  
  24. spring:  
  25.       datasource:  
  26.           url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8  
  27.           username: xuan  
  28.           password: 123456  
  29.           # 使用druid数据源  
  30.           type: com.alibaba.druid.pool.DruidDataSource  
  31.           driver-class-name: com.mysql.cj.jdbc.Driver  
  32.           filters: stat  
  33.           maxActive: 20  
  34.           initialSize: 1  
  35.           maxWait: 60000  
  36.           minIdle: 1  
  37.           timeBetweenEvictionRunsMillis: 60000  
  38.           minEvictableIdleTimeMillis: 300000  
  39.           validationQuery: select 'x'  
  40.           testWhileIdle: true  
  41.           testOnBorrow: false  
  42.           testOnReturn: false  
  43.           poolPreparedStatements: true  
  44.           maxOpenPreparedStatements: 20  
  45.   
  46. #spring-boot整合mybatis  
  47. mybatis:  
  48.   #config-location: classpath:/mapper/config/mybatisConfig.xml #可以注射掉,没用到该配置文件  
  49.   mapper-locations: classpath:/mapper/*Mapper.xml  
  50.   #type-aliases-package: com.xuan.entity  

2、maven的配置文件pom.xml (加入springboot整合mybatis的jar包,以及mysql驱动jar包,阿里的数据库链接池jar包)

以及mybatis 自动生成代码的一些配置

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0modelVersion>  
  5.   
  6.     <groupId>com.xuangroupId>  
  7.     <artifactId>myspringbootartifactId>  
  8.     <version>0.0.1-SNAPSHOTversion>  
  9.     <packaging>jarpackaging>  
  10.   
  11.     <name>myspringbootname>  
  12.     <description>Demo project for Spring Bootdescription>  
  13.   
  14.     <parent>  
  15.         <groupId>org.springframework.bootgroupId>  
  16.         <artifactId>spring-boot-starter-parentartifactId>  
  17.         <version>1.5.8.RELEASEversion>  
  18.         <relativePath/>   
  19.     parent>  
  20.   
  21.     <properties>  
  22.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  23.         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>  
  24.         <java.version>1.8java.version>  
  25.   
  26.         <mybatis.generator.version>1.3.2mybatis.generator.version>  
  27.         <mysql.connector.java.version>6.0.6mysql.connector.java.version>  
  28.   
  29.   
  30.           
  31.           
  32.           
  33.         <classPathEntry.mysql.location>/Users/chenqixuan/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jarclassPathEntry.mysql.location>  
  34.         <javaModelGenerator.targetProject>${basedir}/src/main/javajavaModelGenerator.targetProject>  
  35.         <sqlMapGenerator.targetProject>${basedir}/src/main/resourcessqlMapGenerator.targetProject>  
  36.         <javaClientGenerator.targetProject>${basedir}/src/main/javajavaClientGenerator.targetProject>  
  37.         <mybatis.generator.configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xmlmybatis.generator.configurationFile>  
  38.         <mybatis.generator.overwrite>truemybatis.generator.overwrite>  
  39.     properties>  
  40.   
  41.     <dependencies>  
  42.           
  43.         <dependency>  
  44.             <groupId>org.springframework.bootgroupId>  
  45.             <artifactId>spring-boot-starter-data-redisartifactId>  
  46.         dependency>  
  47.           
  48.         <dependency>  
  49.             <groupId>org.mybatis.spring.bootgroupId>  
  50.             <artifactId>mybatis-spring-boot-starterartifactId>  
  51.             <version>1.3.1version>  
  52.         dependency>  
  53.           
  54.         <dependency>  
  55.             <groupId>mysqlgroupId>  
  56.             <artifactId>mysql-connector-javaartifactId>  
  57.             <version>${mysql.connector.java.version}version>  
  58.         dependency>  
  59.           
  60.         <dependency>  
  61.             <groupId>com.alibabagroupId>  
  62.             <artifactId>druidartifactId>  
  63.             <version>1.0.11version>  
  64.         dependency>  
  65.   
  66.         <dependency>  
  67.             <groupId>org.springframework.bootgroupId>  
  68.             <artifactId>spring-boot-starter-webartifactId>  
  69.         dependency>  
  70.   
  71.           
  72.         <dependency>  
  73.             <groupId>org.springframework.bootgroupId>  
  74.             <artifactId>spring-boot-starter-thymeleafartifactId>  
  75.         dependency>  
  76.         <dependency>  
  77.             <groupId>net.sourceforge.nekohtmlgroupId>  
  78.             <artifactId>nekohtmlartifactId>  
  79.             <version>1.9.22version>  
  80.         dependency>  
  81.   
  82.         <dependency>  
  83.             <groupId>org.springframework.bootgroupId>  
  84.             <artifactId>spring-boot-starter-testartifactId>  
  85.             <scope>testscope>  
  86.         dependency>  
  87.     dependencies>  
  88.   
  89.     <build>  
  90.         <plugins>  
  91.             <plugin>  
  92.                 <groupId>org.springframework.bootgroupId>  
  93.                 <artifactId>spring-boot-maven-pluginartifactId>  
  94.             plugin>  
  95.               
  96.             <plugin>  
  97.                 <groupId>org.mybatis.generatorgroupId>  
  98.                 <artifactId>mybatis-generator-maven-pluginartifactId>  
  99.                 <version>${mybatis.generator.version}version>  
  100.             plugin>  
  101.         plugins>  
  102.     build>  
  103.   
  104.   
  105. project>  

3、mybatis的配置文件 (可以不要需要,因为在application.yml配置了)

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2.         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3.         "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4.   
  5. <configuration>  
  6.     <settings>  
  7.           
  8.         <setting name="lazyLoadingEnabled" value="false" />  
  9.     settings>  
  10.   
  11.     <mappers>  
  12.           
  13.           
  14.     mappers>  
  15.   
  16. configuration>  

4、用户实体类xml映射文件 UserMapper.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <mapper namespace="com.xuan.mapper.UserMapper">  
  4.   
  5.       
  6.     <select id="getAllUser" resultType="com.xuan.entity.User">  
  7.           SELECT * from user  
  8.     select>  
  9.       
  10. mapper>  

5、自动生成配置文件 generatorConfig.xml(项目目录下运行命令: mvn mybatis-generator:generate -e )

注意:generatorConfig.xml配置文件中有些参数笔者是配置在pom.xml里的,另外mysql-connector-java-5.1.38.jar  数据库mysql驱动也不要使用太高的版本(笔者使用6.0.6版本是遇到错误,换成低版本就没事)

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2.         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  3.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  4.   
  5. <generatorConfiguration>  
  6.     <classPathEntry location="${classPathEntry.mysql.location}" />  
  7.   
  8.     <context id="mysqlTables" targetRuntime="MyBatis3">  
  9.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  10.                         connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"  
  11.                         userId="xuan" password="123456">  
  12.         jdbcConnection>  
  13.   
  14.         <javaTypeResolver>  
  15.             <property name="forceBigDecimals" value="false" />  
  16.         javaTypeResolver>  
  17.   
  18.         <javaModelGenerator targetPackage="com.xuan.entity"  
  19.                             targetProject="${javaModelGenerator.targetProject}">  
  20.             <property name="enableSubPackages" value="true" />  
  21.             <property name="trimStrings" value="true" />  
  22.         javaModelGenerator>  
  23.   
  24.         <sqlMapGenerator targetPackage="mapper"  
  25.                          targetProject="${sqlMapGenerator.targetProject}">  
  26.             <property name="enableSubPackages" value="true" />  
  27.         sqlMapGenerator>  
  28.   
  29.         <javaClientGenerator type="XMLMAPPER"  
  30.                              targetPackage="com.xuan.mapper" targetProject="${javaClientGenerator.targetProject}">  
  31.             <property name="enableSubPackages" value="false" />  
  32.         javaClientGenerator>  
  33.   
  34.           
  35.         <table tableName="video" domainObjectName="Video">table>  
  36.   
  37.   
  38.     context>  
  39. generatorConfiguration>  

6、用户实体类:

[java]  view plain  copy
  1. package com.xuan.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * Created by chenqixuan on 17/10/24. 
  7.  */  
  8. public class User implements Serializable{  
  9.     private static final long serialVersionUID = 2120869894112984147L;  
  10.   
  11.     private Integer id;  
  12.   
  13.     private String  name;  
  14.   
  15.     private  Integer age;  
  16.   
  17.     private String address;  
  18.   
  19.     public Integer getId() {  
  20.         return id;  
  21.     }  
  22.   
  23.     public void setId(Integer id) {  
  24.         this.id = id;  
  25.     }  
  26.   
  27.     public String getName() {  
  28.         return name;  
  29.     }  
  30.   
  31.     public void setName(String name) {  
  32.         this.name = name;  
  33.     }  
  34.   
  35.     public Integer getAge() {  
  36.         return age;  
  37.     }  
  38.   
  39.     public void setAge(Integer age) {  
  40.         this.age = age;  
  41.     }  
  42.   
  43.     public String getAddress() {  
  44.         return address;  
  45.     }  
  46.   
  47.     public void setAddress(String address) {  
  48.         this.address = address;  
  49.     }  
  50. }  

7、dao层是使用@Mapper注解接口代理,以前spring是使用自动扫描

[java]  view plain  copy
  1. package com.xuan.mapper;  
  2.   
  3. import com.xuan.entity.User;  
  4. import org.apache.ibatis.annotations.Mapper;  
  5.   
  6. import java.util.List;  
  7.   
  8. /** 
  9.  * Created by chenqixuan on 17/10/24. 
  10.  */  
  11. @Mapper  
  12. public interface UserMapper {  
  13.   
  14.     public List getAllUser();  
  15.   
  16. }  

8、controller引用:

[java]  view plain  copy
  1. package com.xuan.controller;  
  2.   
  3. import com.xuan.entity.User;  
  4. import com.xuan.mapper.UserMapper;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.web.bind.annotation.GetMapping;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. import java.util.*;  
  11.   
  12. /** 
  13.  * Created by chenqixuan on 17/10/24. 
  14.  */  
  15. @RestController  
  16. @RequestMapping("/user")  
  17. public class UserController {  
  18.   
  19.     @Autowired  
  20.     private UserMapper userMapper;  
  21.   
  22.     @GetMapping(value="/all")  
  23.     public List getAllUser(){  
  24.   
  25.         return userMapper.getAllUser();  
  26.     }  
  27.   
  28.   
  29. }  

9、效果图:

SpringBoot整合Mybatis 以及 mybatis自动生成代码配置_第1张图片

10、目录结构:

SpringBoot整合Mybatis 以及 mybatis自动生成代码配置_第2张图片






你可能感兴趣的:(Mybatis,Spring)