在springboot没有出现之前,spring整合mybatis是相当麻烦,各种bean配置,现在我们来感受一下Springboot整合Mybatis是有多么方便
先来看看效果
添加用户:
查询用户:
开始动手
第一步创建springboot项目
到这里项目就创建完成了,我们可以看看pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
动手创建类
首先看看我的目录结构:
创建model user
package com.hawk.yoga.model;
import lombok.Data;
/**
* @Author:
* @Description: user
* @Date: Create in 2019/7/13
*/
@Data
public class User {
private Integer age;
private String name;
//实际项目中不用Double,用更精确的
private Double money;
}
创建统一返回结果
package com.hawk.yoga.common.result;
import com.hawk.yoga.common.CodeEnum;
import lombok.Data;
/**
* @Author:
* @Description: 自定义返回结果
* @Date: Create in 2019/7/13
*/
@Data
public class YogaResult<T> {
private String code;
private T data;
private YogaResult(){
code = CodeEnum.OK.getCode().toString();
}
public static YogaResult ok(){
return new YogaResult();
}
public static <T>YogaResult ok(T data){
YogaResult yogaResult = new YogaResult();
yogaResult.setData(data);
return yogaResult;
}
}
创建code表
package com.hawk.yoga.common;
import lombok.Data;
import lombok.Getter;
/**
* @Author:
* @Description:
* @Date: Create in 2019/7/13
*/
public enum CodeEnum {
/**
* 2开通频段为成功
*/
OK("ok",2000),
/**
* 4开头频段为错误
*/
ERROR("error",4000);
CodeEnum(String desc,Integer code){
this.code = code;
this.desc = desc;
}
@Getter
private String desc;
@Getter
private Integer code;
}
创建controller
package com.hawk.yoga.controller;
import com.hawk.yoga.common.result.YogaResult;
import com.hawk.yoga.model.User;
import com.hawk.yoga.model.dto.UserDto;
import com.hawk.yoga.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author:
* @Description: demo
* @Date: Create in 2019/7/13
*/
@RestController
@RequestMapping("/user")
public class TestController {
@Autowired
private UserService userService;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public YogaResult addUser(UserDto userDto) {
userService.add(userDto);
return YogaResult.ok();
}
@RequestMapping(value = "/find", method = RequestMethod.GET)
public YogaResult<User> addUser(String name) {
User userByName = userService.findUserByName(name);
return YogaResult.ok(userByName);
}
}
service
package com.hawk.yoga.service;
import com.hawk.yoga.mapper.UserMapper;
import com.hawk.yoga.model.User;
import com.hawk.yoga.model.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author:
* @Description:
* @Date: Create in 2019/7/13
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public void add(UserDto userDto) {
userMapper.add("yoga", 22, 3000.0);
}
@Override
public User findUserByName(String name) {
return userMapper.findUserByName(name);
}
}
public interface UserService {
/**
* add
*/
void add(UserDto userDto);
/**
*
* @param name
* @return
*/
User findUserByName(String name);
}
mapper
@Mapper
public interface UserMapper {
/**
* @param name
* @param age
* @param money
* @return
*/
@Insert("INSERT INTO user(name, age,money) VALUES(#{name}, #{age}, #{money})")
Integer add(@Param("name") String name, @Param("age") Integer age, @Param("money") Double money);
/**
* @param name
* @return
*/
User findUserByName(String name);
}
添加方法使用的是注解,简单应用推荐第一种,一般使用第二中通过xml配置
userMapper.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.hawk.yoga.mapper.UserMapper">
<select id="findUserByName" parameterType="java.lang.String" resultType="com.hawk.yoga.model.User">
SELECT * FROM user WHERE name = #{name}
</select>
</mapper>
实际应用中不要用*,会导致扫描全表。
配置文件 application.properties
server.port=8520
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/item?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
数据库
CREATE TABLE `user` (
`id` int(13) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(33) DEFAULT NULL COMMENT '姓名',
`age` int(3) DEFAULT NULL COMMENT '年龄',
`money` double DEFAULT NULL COMMENT '账户余额',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
到此项目已经完成,可以运行起来了,其中有一点不规范的地方就是service哪里,我直接写死了数据,只是为了测试起来方便,最后一点在启动应用上加一个注解:扫描mapper
是不是很轻松呢,感谢你的阅读,不当之处请指教。