SpringBoot之整合MyBatis
Posted 北极星的眼泪520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot之整合MyBatis相关的知识,希望对你有一定的参考价值。
今天了解一下SpringBoot如何与我们最常用的ORM框架Mybatis整合。
一、
需要在pom.xml文件里加入mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
二、
需要在application.properties文件里加关于mybatis的配置
#mybatis配置
mybatis.mapper-locations=classpath:mapper/*.xml ------意思是mapper文件在classpath下的mapper里
mybatis.type-aliases-package=com.renruibin.springbootmybatis.pojo -------设置自定义的Pojo别名为首写字母小写的pojo
#数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootmybatis
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=rrb
三、
使用Ant,达到自动创建默认的Mybatis代码和生成表,需要添加以下文件,目录结构为:
1、user.xml,这是要自动生成代码的一些配置
<?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="user">
<jdbcConnection driverClass="${database.driver}"
connectionURL="${database.url}" userId="${database.username}"
password="${database.password}" />
<javaModelGenerator targetPackage="com.renruibin.springbootmybatis.pojo"
targetProject="${targetProject}" />
<sqlMapGenerator targetPackage="mapper"
targetProject="${targetProjRsrc}" />
<javaClientGenerator targetPackage="com.renruibin.springbootmybatis.dao"
targetProject="${targetProject}" type="XMLMAPPER" />
<table tableName="user" domainObjectName="User">
</table>
</context>
</generatorConfiguration>
javaModelGenerator:配置自动生成的pojo所在的位置
sqlMapGenerator:配置自动生成的mapper.xml文件所在的位置
javaClientGenerator:配置自动生成的mapper.java文件所在的位置
2、changleLog.xml,这是要自动生成表或者修改表结构的一些配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="20190113" author="rrb">
<createTable tableName="user" remarks="用户版">
<column name="id" type="varchar(32)">
<constraints nullable="false" primaryKey="true" />
</column>
<column name="username" remarks="用户名" type="varchar(50)" />
<column name="password" remarks="密码" type="varchar(50)" />
<column name="status" remarks="状态" type="int" />
<column name="createtime" remarks="创建时间" type="TIMESTAMP" />
<column name="edittime" remarks="修改时间" type="TIMESTAMP" />
</createTable>
</changeSet>
<changeSet id="20190113-2" author="rrb">
<dropColumn tableName="user" columnName="createtime"></dropColumn>
<dropColumn tableName="user" columnName="edittime"></dropColumn>
</changeSet>
</databaseChangeLog>
3、database.properties,这是数据源的一些配置
database.driver=com.mysql.jdbc.Driver
database.url=jdbc\\:mysql\\://127.0.0.1\\:3306/springbootmybatis?useUnicode\\=true&characterEncoding\\=UTF-8
database.username=root
database.password=rrb
4、update&mybatis.xml,这是ant自动生成代码的配置
<project name="updateDatabase" default="mybatis">
<property file="database.properties">
</property>
<property name="targetProject" location="..\\src\\main\\java">
</property>
<property name="targetProjRsrc" location="..\\src\\main\\resources">
</property>
<path id="jdbclib">
<fileset dir="${basedir}\\lib" includes="mysql-connector-java-5.1.23-bin.jar" />
</path>
<target name="mybatis" description="Generate the files">
<taskdef name="mbgenerator" classname="org.mybatis.generator.ant.GeneratorAntTask">
<classpath>
<pathelement location="${basedir}\\lib\\mybatis-generator-core-1.3.2.jar" />
<pathelement location="${basedir}\\lib\\generator-0.0.1-SNAPSHOT.jar" />
<pathelement location="${basedir}\\lib\\mysql-connector-java-5.1.23-bin.jar" />
</classpath>
</taskdef>
<mbgenerator overwrite="true" configfile="generator/user.xml"
verbose="true">
<propertyset>
<propertyref name="database.driver" />
<propertyref name="database.url" />
<propertyref name="database.username" />
<propertyref name="database.password" />
<propertyref name="targetProject" />
<propertyref name="targetProjRsrc" />
</propertyset>
</mbgenerator>
</target>
</project>
5、updateDatabase.xml,这是ant自动生成表的配置<project name="updateDatabase" default="updateDatabase">
<property file="database.properties">
</property>
<path id="jdbclib">
<fileset dir="${basedir}\\lib" includes="mysql-connector-java-5.1.23-bin.jar" />
</path>
<target name="updateDatabase">
<fail unless="database.url">database.url not set</fail>
<fail unless="database.username">database.username not set</fail>
<fail unless="database.password">database.password not set</fail>
<taskdef resource="liquibasetasks.properties">
<classpath>
<pathelement location="${basedir}\\lib\\snakeyaml-1.13.jar" />
<pathelement location="${basedir}\\lib\\liquibase.jar" />
</classpath>
</taskdef>
<updateDatabase
changeLogFile="${basedir}\\changeLog.xml"
driver="${database.driver}"
url="${database.url}"
username="${database.username}"
password="${database.password}"
promptOnNonLocalDatabase="false"
dropFirst="false"
classpathref="jdbclib"
loglevel="info"
/>
</target>
</project>
四、
建立dao、service、controller,目录如下:
1、先看一下dao层的代码是如何编写的吧
@Mapper
public interface UserAtomMapper {
List<User> finaAll();
}
@Mapper这个注解很关键,之前用的@Repository注解,但是与springboot整合之后,这个注解就不能用了,只有@Mapper注解的时候,才可以把这个类注入到service里。
2、看一下mapper层的xml文件是如何编写的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.renruibin.springbootmybatis.dao.UserAtomMapper">
<select id="finaAll" resultType="com.renruibin.springbootmybatis.pojo.User">
select * from user
</select>
</mapper>
3、看一下service层的代码
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserAtomMapper userAtomMapper;
@Override
public List<User> findAll() {
return userAtomMapper.finaAll();
}
4、看一下controller层的代码
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 获取所有的用户
* @return
*/
@RequestMapping(value = "findAll",method = RequestMethod.GET)
public List<User> findAll(){
return userService.findAll();
}
}
使用了注解@RestController,这个注解相当于两个注解,@Controller和@ResponseBody
五、
接下来可以测试,在浏览器输入localhost:8080/findAll
以上是关于SpringBoot之整合MyBatis的主要内容,如果未能解决你的问题,请参考以下文章
springboot整合mybatis-plus+durid数据库连接池
springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件