7.整合mybatis框架

1.依赖导入

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
<!--整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

2.配置文件配置mapper的位置以及实体类的别名

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#整合mybatis
mybatis.type-aliases-package=com.wu.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

3.创建实体类以及mapper接口

@Mapper//表示这是一个mybatis的mapper类  或者在测试类使用@MapperScan扫描包
@Repository//交给spring容器管理是用在持久层的接口上
public interface UserMapper {
    List<User> queryUserList();
    User queryUserById(int id);
    int addUser(User user);
    int updateUser(User user);
    int deleteUser(int id);
}

4.在如下位置创建UserMapper

7.整合mybatis框架

 

 5.UserMapper

<?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.wu.mapper.UserMapper">
    <select id="queryUserList" resultType="User">
        select * from user
    </select>
    <select id="queryUserById" resultType="User">
        select * from user where id =#{id}
    </select>
    <insert id="addUser" parameterType="User">
        insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
    </insert>
    <update id="updateUser" parameterType="User">
        update user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>
    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>
</mapper>

6.编写controller类进行测试

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/list")
    public List<User> queryUserList() {
        List<User> userList = userMapper.queryUserList();
        userList.forEach(System.out::println);
        return userList;
    }

    @GetMapping("/find/{id}")
    public User find(@PathVariable("id") int id) {
        User user = userMapper.queryUserById(id);
        return user;
    }

    @GetMapping("/add")
    public String add() {
        userMapper.addUser(new User(10, "*min", "123456"));
        return "ok";
    }

    @GetMapping("update")
    public String update() {
        userMapper.updateUser(new User(10, "*min2", "123456"));
        return "ok";
    }
    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id") int id) {
        userMapper.deleteUser(id);
        return "ok";
    }
}

 

7.整合mybatis框架

上一篇:svn


下一篇:centos7 新增加路由