介绍一下SpringBoot整合mybatis,数据库选用的是mysql。
首先创建数据库
CREATE DATABASE test;
建表以及插入初始数据(sql是从navicat中导出的)
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of user
-- ----------------------------
BEGIN;
INSERT INTO `user` VALUES (1, 'dalaoyang', '13');
INSERT INTO `user` VALUES (2, 'xiaoli', '123');
INSERT INTO `user` VALUES (3, 'xiaoxiongmao', '123');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
下图为项目目录结构,
java---
controller包负责测试整合
dao包作为数据操作层
entity作为数据实体类
resources---
mapper写dao层对应实现的sql
mybatis里面是mybatis配置,包含typeAlias等等
sql里面放的是上面写的建表数据及sql
接下来直接上代码,启动类没有修改,代码如下
package com.dalaoyang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
application.properties包含了数据库配置,mybatis配置,代码如下:
##端口号
server.port=8888
##检查 mybatis 配置是否存在,一般命名为 mybatis-config.xml
mybatis.check-config-location =true
##配置文件位置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
## mapper xml 文件地址
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
##日志级别
logging.level.com.yang.dao=debug
##数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=root
##数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
实体类User
package com.dalaoyang.entity;
import org.apache.ibatis.type.Alias;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.entity
* @email [email protected]
* @date 2018/4/5
*/
@Alias("user")
public class User {
private int id;
private String user_name;
private String user_password;
public User(String user_name, String user_password) {
this.user_name = user_name;
this.user_password = user_password;
}
public User(int id, String user_name, String user_password) {
this.id = id;
this.user_name = user_name;
this.user_password = user_password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
}
dao层代码
package com.dalaoyang.dao;
import com.dalaoyang.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dao
* @email [email protected]
* @date 2018/4/5
*/
@Mapper
public interface UserMapper {
User findUserByUsername(String username);
void updateUserByUsername(User user);
void deleteUserByUsername(String username);
void saveUser(User user);
List getUserList();
}
UserMapper.xml代码
UPDATE USER SET USER_PASSWORD=#{user_password} WHERE USER_NAME=#{user_name}
DELETE FROM USER WHERE USER_NAME=#{1}
INSERT INTO USER (user_password,user_name) VALUES (#{user_password},#{user_name})
mybatis-config.xml
pom文件
4.0.0
com.dalaoyang
springboot_mybatis
0.0.1-SNAPSHOT
jar
springboot_mybatis
springboot_mybatis
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
UserController
package com.dalaoyang.controller;
import com.dalaoyang.dao.UserMapper;
import com.dalaoyang.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email [email protected]
* @date 2018/4/5
*/
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
//http://localhost:8888/getUser?username=xiaoli2
@RequestMapping("/getUser")
public String getUser(String username){
User user =userMapper.findUserByUsername(username);
return user!=null ? username+"的密码是:"+user.getUser_password():"不存在用户名为"+username+"的用户";
}
//http://localhost:8888/updateUser?username=xiaoli2&password=123
@RequestMapping("/updateUser")
public String updateUser(String password,String username){
User user = new User(username,password);
userMapper.updateUserByUsername(user);
return "success!";
}
//http://localhost:8888/addUser?username=xiaoli2&password=123
@RequestMapping("/addUser")
public String addUser(String username,String password){
User user = new User(username,password);
userMapper.saveUser(user);
return "success!";
}
//http://localhost:8888/addUser?username=xiaoli2
@RequestMapping("/deleteUser")
public String deleteUser(String username){
userMapper.deleteUserByUsername(username);
return "success!";
}
//http://localhost:8888/getUserList
@RequestMapping("/getUserList")
public List getUserList(String username, String password){
return userMapper.getUserList();
}
}
启动项目,访问controller上面对应的注释上的地址即可以测试,
其中包含了简单的增删改查,SpringBoot整合Mybatis就这样完成了。
源码下载 :大老杨码云