今天我们在springboot上集成mybatis。首先创建一个maven项目。
添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!--springboot依赖-->
<dependency>
<groupid>org.springframework.boot<groupi>
<artifactid>springbootstarter<artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<!--测试-->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
<!--集成mybatis-->
<dependency>
<groupid>org.mybatis.spring.boot</groupid>
<artifactid>mybatis-spring-boot-starter<artifactid>
<version> 1.3 . 0 </version>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version> 5.1 . 35 </version>
</dependency>
|
在src/main/resources/目录下新建一个application.properties配置文件,里面写数据源的配置
1
2
3
4
|
spring.datasource.name=test
spring.datasource.url=jdbc:mysql: //127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=root
|
配置文件有两种写法,我们还可以写成yml文件形式,在src/main/resources/目录下新建一个application.yml文件(两种方式选其中一种就可以了)
1
2
3
4
5
6
|
spring:
datasource:
url: jdbc:mysql: //127.0.0.1:3306/test
username: root
password: root
driver- class -name: com.mysql.cj.jdbc.driver
|
在本地数据库中创建test数据库,并新建一个表t_user
1
2
3
4
5
6
7
8
|
create database test;
create table `t_user` (
`user_id` int ( 11 ) not null auto_increment,
`user_name` varchar( 225 ) not null ,
`password` varchar( 225 ) not null ,
`phone` varchar( 225 ) not null ,
primary key (`user_id`)
) engine=innodb auto_increment= 1000 default charset=utf8
|
这里我们使用mybatis-generator插件,自动生成实体类,mapper,以及mapper.xml文件
在pom中添加build依赖(放在层次外面)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering> true </filtering>
<excludes>
<exclude>generator/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupid>org.mybatis.generator</groupid>
<artifactid>mybatis-generator-maven-plugin</artifactid>
<version> 1.3 . 4 -snapshot</version>
<dependencies>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version> 5.1 . 39 </version>
</dependency>
<dependency>
<groupid>org.mybatis.generator</groupid>
<artifactid>mybatis-generator-core</artifactid>
<version> 1.3 . 4 -snapshot</version>
</dependency>
</dependencies>
<configuration>
<overwrite> true </overwrite>
<configurationfile>src/main/resources/generator/generatorconfig.xml</configurationfile>
</configuration>
</plugin>
</plugins>
</build>
|
在src/main/resources/下面新建一个目录generator,在此目录下新建一个generatorconfig.xml文件(注意根据自己的目录,调整路径)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!doctype generatorconfiguration
public "-//mybatis.org//dtd mybatis generator configuration 1.0//en" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorconfiguration>
<context id= "db2tables" targetruntime= "mybatis3" >
<commentgenerator>
<property name= "suppressdate" value= "true" />
<property name= "suppressallcomments" value= "true" />
</commentgenerator>
<!--数据库链接地址账号密码-->
<jdbcconnection driverclass= "com.mysql.jdbc.driver"
connectionurl= "jdbc:mysql://127.0.0.1:3306/test"
userid= "root"
password= "root" >
</jdbcconnection>
<javatyperesolver>
<property name= "forcebigdecimals" value= "false" />
</javatyperesolver>
<!--生成model类存放位置-->
<javamodelgenerator targetpackage= "com.lw.study.dao.domain" targetproject= "src/main/java" >
<property name= "enablesubpackages" value= "true" />
<property name= "trimstrings" value= "true" />
</javamodelgenerator>
<!--生成映射文件存放位置-->
<sqlmapgenerator targetpackage= "sqlmap" targetproject= "src/main/resources/" >
<property name= "enablesubpackages" value= "true" />
</sqlmapgenerator>
<!--生成dao类存放位置-->
<!-- 客户端代码,生成易于使用的针对model对象和xml配置文件 的代码
type= "annotatedmapper" ,生成java model 和基于注解的mapper对象
type= "mixedmapper" ,生成基于注解的java model 和相应的mapper对象
type= "xmlmapper" ,生成sqlmap xml文件和独立的mapper接口
-->
<javaclientgenerator type= "xmlmapper" targetpackage= "com.lw.study.dao.mapper" targetproject= "src/main/java" >
<property name= "enablesubpackages" value= "true" />
</javaclientgenerator>
<!--生成对应表及类名-->
<!--<table schema= "loandb" tablename= "t_audit_job" domainobjectname= "auditjob" >-->
<!--<property name= "useactualcolumnnames" value= "true" />-->
<!--<generatedkey column= "id" sqlstatement= "mysql" identity= "true" />-->
<!--</table>-->
<table tablename= "t_user" domainobjectname= "user" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" ></table>
</context>
</generatorconfiguration>
|
然后运行generator配置文件。
在点击左上角的➕,选择maven
working derectory选择自己的项目路径。然后apply,在右上角运行就可以了,就会在指定的目录下生成三个文件(实体类,mapper类,和mapper.xml文件)
最后在src/main/java/com/lw/study/目录下新建一个mybatisconfig目录,里面新建两个配置类。
mybatisconfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@configuration
@enabletransactionmanagement
public class mybatisconfig {
@bean
@configurationproperties (prefix = "spring.datasource" )
public datasource datasource(){
return new org.apache.tomcat.jdbc.pool.datasource();
}
@bean (name = "sqlsessionfactory" )
public sqlsessionfactory sqlsessionfactorybean() throws exception {
sqlsessionfactorybean sqlsessionfactorybean = new sqlsessionfactorybean();
sqlsessionfactorybean.setdatasource(datasource());
pathmatchingresourcepatternresolver resolver = new pathmatchingresourcepatternresolver();
sqlsessionfactorybean.setmapperlocations(resolver.getresources( "classpath:/sqlmap/*.xml" ));
return sqlsessionfactorybean.getobject();
}
}
|
mybatismapperscannerconfig.java
1
2
3
4
5
6
7
8
9
10
11
|
@configuration
@autoconfigureafter (mybatisconfig. class )
public class mybatismapperscannerconfig {
@bean
public mapperscannerconfigurer mapperscannerconfigurer() {
mapperscannerconfigurer mapperscannerconfigurer = new mapperscannerconfigurer();
mapperscannerconfigurer.setsqlsessionfactorybeanname( "sqlsessionfactory" );
mapperscannerconfigurer.setbasepackage( "com.study.dao.mapper" );
return mapperscannerconfigurer;
}
}
|
创建一个springboot的启动类,application.java
1
2
3
4
5
6
7
|
@springbootapplication (scanbasepackages = { "com.lw.study.*" })
@mapperscan ( "com.lw.study.mapper" )
public class application {
public static void main(string[] args) {
springapplication.run(application. class ,args);
}
}
|
好了,到这里我们在springboot中通过generator插件的方式集成mybatis就完成了。大家可以自己写一个测试类,使用mapper中的方法看能否在数据库中查到数据。
总结
以上所述是小编给大家介绍的创建springboot工程并集成mybatis的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/Soda_lw/article/details/78980720