1.1 需求
通过Spring Boot Web项目api接口的方式,整合MyBatis实现crud的操作。
1.2 创建Spring Boot Web项目
重温一下web项目创建的过程。
1.3 引入项目中需要的starter依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.4 创建数据库表
db_violentayang_springboot—>t_user
1.5 创建domain/User对象
public class User {
private int id;
private String username;
private String password;
private String number;
...
}
1.6 开发dao层
@Repository
@Mapper
public interface UserMapper {
User find(String username);
List<User> list();
int insert(User user);
int delete(int id);
int update(User user);
}
1.7 开发service层
@Service
public class UserService {
@Autowired private UserMapper userMapper;
public User findByUsername( String username )
{
return(userMapper.find( username ) );
}
public List<User> listUser()
{
return(userMapper.list() );
}
public int insertUser( User user )
{
return(userMapper.insert( user ) );
}
public int updateUser( User user )
{
return(userMapper.update( user ) );
}
public int delete( int id )
{
return(userMapper.delete( id ) );
1.8 开发controller层
@RestController @RequestMapping( value = "/user", method = { RequestMethod.GET, RequestMethod.POST } ) public class UserController {
@Autowired private UserService userService;
@RequestMapping( "/listone" ) @ResponseBody public User listOne( String username )
{
return(userService.findByUsername( username ) );
< ? xml version = "1.0" encoding = "UTF-8" ? > < !DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < mapper namespace = "com.gupao.springbootdemo.dao.UserMapper" > < resultMap id = "result" type = "com.gupao.springbootdemo.domain.User" > < result property = "username" column = "username" / >
}
@RequestMapping( "/listall" ) @ResponseBody public List<User> listAll()
{
return(userService.listUser() );
}
@RequestMapping( value = "/add", method = RequestMethod.POST ) @ResponseBody public String add( User user )
{
int result = userService.insertUser( user );
if ( result >= 1 )
{
return("添加成功");
} else{
return("添加失败");
}
}
@RequestMapping( value = "/update", method = RequestMethod.POST ) @ResponseBody public String update( User user )
{
int result = userService.updateUser( user );
if ( result >= 1 )
{
return("修改成功");
} else{
return("修改失败");
}
}
@RequestMapping( value = "/delete", method = RequestMethod.GET ) @ResponseBody public String delete( int id )
{
int result = userService.delete( id );
if ( result >= 1 )
{
return("删除成功");
} else{
return("删除失败");
}
}
}
1.9 resources目录下创建mapper文件夹—UserMapper.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.violentayang.springbootdemo.dao.UserMapper">
<resultMap id="result" type="com.violentayang.springbootdemo.domain.User">
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="number" column="number"/>
</resultMap>
<select id="find" resultMap="result"> SELECT * FROM t_user where username=#{username} </select>
<select id="list" resultMap="result"> SELECT * FROM t_user </select>
<insert id="insert" parameterType="com.violentayang.springbootdemo.domain.User" keyProperty="id" useGeneratedKeys="true"> INSERT INTO t_user (id,username,password,number )VALUES ( #{id}, #{username, jdbcType=VARCHAR}, #{password, jdbcType=VARCHAR}, #{number} )</insert>
<delete id="delete"parameterType="int"> delete from t_user where id=#{id} </delete>
<update id="update" parameterType="com.violentayang.springbootdemo.domain.User"> update t_user set username=#{username},password=#{password},number=#{number} where id=#{id} </update>
</mapper>
1.10 application.yml文件
spring:
datasource:
url: jdbc:mysql://39.100.39.63:3306/db_gupao_springboot? useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
server:
port: 8888
1.11 启动项目测试
查询
http://localhost:8888/user/listone?username=violentayang
全部查询
http://localhost:8888/user/listall
增加
http://localhost:8888/user/add?id=3&username=AAA&password=111111&number=300
更新
http://localhost:8888/user/update?id=3&username=BBB
删除 http://localhost:8888/user/delete?id=3
|