SpringBoot - 03 整合MyBatis
1、导入相关包
主要包括:
- spring-boot-starter-logging
- spring-boot-starter-jdbc
- mysql-connector-java
- mybatis-spring-boot-starter
- lombok
- log4j
完整的pom文件代码:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.rui</groupId> <artifactId>03QuickMyBatis</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <java.version>1.8</java.version> </properties> <!-- 添加SpringBoot的基础工程为父工程,这个项目会把包之间的依赖关系处理好,不需要我们处理了 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> </parent> <dependencies> <!--热启动的包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!--测试包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--SpringMVC Web的启动器,会自动引入很多的Maven包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--thymeleaf页面模板--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--日志组件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!--jdbc依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--MySQL连接的依赖包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!--myBatis引入--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!--简化Bean的代码,自动生成get/set方法--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> <!--日志输出--> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <!--用于springboot打包,把资源,内部包,静态文件一起打包到一个Jar包中--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
2、myBatis对接
mybatis组件对接
通过application.yml配置myBatis参数属性和数据库连接
spring: #热部署 devtools: restart: enabled: true #数据源连接信息 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://47.100.229.xxx:3306/springStudy?useSSL=false&serverTimezone=UTC username: root password: xxx #myBatis配置(主配置文件和Mapper文件) mybatis: config-location: classpath:mybatis/mybatis.xml mapper-locations: classpath:mybatis/mapper/*.xml
mybatis主配置文件
在resources目录下创建mybatis目录,内部创建mybatis.xml主配置文件,内部代码如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局配置参数,相关的参数参照myBatis文档 --> <settings> <!-- 是否开启缓存 --> <setting name="cacheEnabled" value="true"/> <!-- 是否开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!--将积极加载修改成消极加载--> <setting name="aggressiveLazyLoading" value="false"/> <!--采用Log4j输出调试信息--> <setting name="logImpl" value="LOG4J"/> </settings> <!-- 类型别名,声明的短名称可以在Mapper文件中替代使用 --> <typeAliases> <!-- 自动为包内所有类创建类型别名,默认是首字母小写的类名称 也可以在类上方通过@Alias("别名")类设置所需要的别名 --> <package name="rui.Mdl"/> </typeAliases> </configuration>
3、日志配置
创建log4j.properties文件
## log4j ### log4j.rootLogger=DEBUG,CONSOLE,LOGFILE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] - %m%n log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.file=log4j.log log4j.appender.LOGFILE.appand=true log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%5p [%t] - %m%n
4、操作例子
4.1定义模型类
package rui.model; import lombok.Data; import org.springframework.stereotype.Component; /*模型层,通过Data 注解,省去了get set 等方法*/ @Component @Data public class Mdl_ex_customer { private String custId; private String custName; private String telephone; private String sex; private String remark; }
4.2定义访问层
package rui.dao; import org.apache.ibatis.annotations.Mapper; import rui.model.Mdl_ex_customer; @Mapper public interface Dao_ex_customer { public Mdl_ex_customer findByKey(String custId); public int insert(Mdl_ex_customer model); }
4.3定义Mapper文件
<?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="rui.dao.Dao_ex_customer"> <select id="findByKey" resultType="rui.model.Mdl_ex_customer"> SELECT * FROM ex_customer where custId=#{custId} </select> <insert id="insert" parameterType="rui.model.Mdl_ex_customer"> insert into ex_customer(custId,custName,telephone,sex,remark) VALUES(#{custId},#{custName},#{telephone},#{sex},#{remark}) </insert> </mapper>
4.4定义业务层
package rui.bll; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import rui.dao.Dao_ex_customer; import rui.model.Mdl_ex_customer; @Service public class Bll_ex_customer { @Autowired private Dao_ex_customer ex_customerDao; public Mdl_ex_customer findByKey(String custId) { return ex_customerDao.findByKey(custId); } public int insert(Mdl_ex_customer model){ return ex_customerDao.insert(model); } }
4.5定义控制器层
package rui.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import rui.model.Mdl_ex_customer; import rui.bll.Bll_ex_customer; @RestController @RequestMapping(value = "hello") public class HelloController { @Autowired private Bll_ex_customer ex_customerBll; @RequestMapping(value = "index") public Mdl_ex_customer index() { Mdl_ex_customer model = ex_customerBll.findByKey("C002"); System.out.println(model.toString()); return model; } @RequestMapping(value="insert") public Mdl_ex_customer insert() { Mdl_ex_customer entry = new Mdl_ex_customer(); entry.setCustId("C003"); entry.setCustName("王志瑞"); entry.setTelephone("1534518xxxx"); entry.setSex("男"); ex_customerBll.insert(entry); return entry; } }
5、项目目录结构