1. 下载默认项目,spring项目初始化,并添加依赖包,如下图:
2. 添加jdbc数据源配置和映射文件application.properties:
spring.application.name=mydemo
logging.path=./logs
spring.datasource.url=jdbc:mysql://localhost/jpa?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=8801
mybatis.type-aliases-package=com.mybatis.springBoot.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
3.新建pojo对象
需要application.properties配置扫描alias声明包mybatis.type-aliases-package=com.mybatis.springBoot.pojo,否则@Alias("user")不生效
package com.mybatis.springBoot.pojo;
import org.apache.ibatis.type.Alias;
@Alias("user")
public class User {
private int id;
private String userName;
private int sex;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4.新建mapper映射接口
package com.mybatis.springBoot.mapper;
import com.mybatis.springBoot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
int insertUser(User user);
}
5.在resources目录新建mapper目录,新建xml文件UserMapper.xml,内容如下
需要声明扫描文件,否则不生效mybatis.mapper-locations=classpath: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.mybatis.springBoot.mapper.UserMapper">
<resultMap id="userMap" type="user">
<result column="id" property="id"/>
<result column="username" property="userName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
</resultMap>
<insert id="insertUser" parameterType="user" useGeneratedKeys="true" keyProperty="id">
insert into user(username,sex,age) value(#{userName},#{sex},#{age})
</insert>
</mapper>
6. 编写测试类
@SpringBootTest
@MapperScan
class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void insertUser() {
User user = new User();
user.setAge(100);
user.setSex(3);
user.setUserName("机器人");
userMapper.insertUser(user);
System.out.println(user.getId());
}
}
7. mvc三层模式
7.1新建service接口
package com.mybatis.springBoot.service;
import com.mybatis.springBoot.pojo.User;
public interface IUserService {
int insertUser(User user);
}
package com.mybatis.springBoot.service.impl;
import com.mybatis.springBoot.mapper.UserMapper;
import com.mybatis.springBoot.pojo.User;
import com.mybatis.springBoot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public int insertUser(User user) {
userMapper.insertUser(user);
return user.getId();
}
}
7.2新建实现类
7.3新建controller调用
package com.mybatis.springBoot.controller;
import com.mybatis.springBoot.pojo.User;
import com.mybatis.springBoot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService iUserService;
@RequestMapping("/insertUser")
int insertUser(User user) {
return iUserService.insertUser(user);
}
}
7.4 新建测试类
package com.mybatis.springBoot;
import com.mybatis.springBoot.mapper.UserMapper;
import com.mybatis.springBoot.pojo.User;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.mybatis.spring.annotation.MapperScan;
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest
@MapperScan
public class ApplicationTests {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Autowired
private UserMapper userMapper;
@Before
public void setMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testUser() {
try {
ResultActions resultActions =
mockMvc.perform(MockMvcRequestBuilders.post("/user/insertUser")
.param("sex", "12")
.param("userName", "张三"));
MvcResult mvcResult = resultActions.andReturn();
byte[] resultByte = mvcResult.getResponse().getContentAsByteArray();
System.out.println(new String(resultByte));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void insertUser() {
User user = new User();
user.setAge(100);
user.setSex(3);
user.setUserName("机器人");
userMapper.insertUser(user);
System.out.println(user.getId());
}
}