第04章—整合Mybatis
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html
码云源码地址:https://gitee.com/jinxiaohang/springboot
SSM框架中接触过Spring整合Mybatis。
一、引入依赖
如果是新建项目的,可以在这页添加依赖;
如果是原有项目,还可以在pom.xml 引入ORM框架(Mybaits-Starter)和数据库驱动(MySQL-Conn)的依赖。
<?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>com.xiaohang</groupId> <artifactId>springboot-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-mybatis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--添加Web依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--添加Test依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加MySQL驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--添加Mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、添加数据源
application.properties也可以配置,但语法上有些不同而已。
在application.yml 添加数据源,以及开启Mybaits的驼峰映射功能。
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
configuration:
map-underscore-to-camel-case: true #开启驼峰映射
三、编写各层代码
// entity类如下: public class User { private String userId; private String username; private String password; // Getters & Setters .. } // dao层代码如下: @Component public interface UserMapper { @Select("select * from user") List<User> list(); @Select("select * from user where userId = #{userId}") User getOne(String userId); @Insert("insert into user(userId,username,password) values(#{userId},#{username},#{password})") boolean save(User user); @Update("update user set username=#{username},password=#{password} where userId=#{userId}") boolean update(User user); @Delete("delete from user where userId = #{userId}") boolean delete(String userId); }
//service层
//serviceImpl层
//controller层
这些都和之前接触的类似,所以不在罗列,具体可以参照上面上传的代码
四、添加数据库记录
mysql> DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `userId` varchar(50) , `username` varchar(50) , `password` varchar(50) ) ; INSERT INTO `user` VALUES ('1', 'admin', 'admin'); INSERT INTO `user` VALUES ('2', 'yizhiwazi', '123456');
五、启动项目
@SpringBootApplication @MapperScan("com.xiaohang.springbootmybatis.dao")//新添加的注解 public class SpringbootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisApplication.class, args); } }
六、测试
运用火狐浏览器或者postman进行测试。
本篇重点关键在于依赖的添加、文件配置、和一些注解的使用,其他都和接触过的ssm、ssh差不多。
因为测试是件很麻烦的事,而且对于这样的接口进行测试,还要与前端的需求统一,所以下一章将学习swagger。