码迷,mamicode.com
首页 > 数据库 > 详细

spring boot + mybaits 操作数据库

时间:2019-05-31 18:08:59      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:org   select   ring   column   can   实体类   update   interface   generate   

spring boot 访问 mysql
方式三:spring boot + mybaits

pom 文件引入 依赖

<!-- mybatis -->
<dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
</dependency>

<!-- mysql驱动 -->
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
</dependency>

配置mybaits

mybatis

# 对应实体类的包名
mybatis.typeAliasesPackage=com.example.demo.model
# mapper.xml文件所在位置
mybatis.mapperLocations=classpath:**/mapper/*.xml

加入 entity mapper service controller 文件
entity

public class User {

        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        private Long id;

        private String name;

        private int age;

        private String sex;

        public Long getId() {
                return id;
        }

        public void setId(Long id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public int getAge() {
                return age;
        }

        public void setAge(int age) {
                this.age = age;
        }

        public String getSex() {
                return sex;
        }

        public void setSex(String sex) {
                this.sex = sex;
        }
}

mapper

@Mapper
public interface UserMapper  {

        User findUserById(Long id);

        int add(User user);

        int update(User user);
}

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.example.demo.dao.mybatis.UserMapper">

        <resultMap id = "BaseResult" type = "com.example.demo.model.User">
                <result property = "id" column = "id"/>
                <result property = "name" column = "name"/>
        </resultMap>
        <select id = "findUserById" resultMap = "BaseResult">
                SELECT * FROM user where id = #{id}
        </select>

        <insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert  into user (name,age,sex) values (#{name},#{age},#{sex})
        </insert>

        <update id = "update" parameterType="com.example.demo.model.User">
                update user set name = #{name} ,age = #{age} ,sex = #{sex} where id = ${id}
        </update>
</mapper>

service

@Service
public class MybatisUserService {

        @Autowired
        UserMapper userDao;

        public int add(User user){
            return   userDao.add(user);
        }

        public int update(User user){
                return   userDao.update(user);
        }

        public User getById(long id){
                return   userDao.findUserById(id);
        }
}

controller

@RestController
@RequestMapping("/mybatisUser")
public class MybatisUserController {

        @Autowired
        MybatisUserService userService;

        @RequestMapping(value = "/add",method = RequestMethod.POST)
        public Object addUser(@RequestBody User user){

                return userService.add(user);
        }

        @RequestMapping(value = "/update",method = RequestMethod.PUT)
        public Object updateUser(@RequestBody User user){

                return userService.update(user);
        }

        @RequestMapping(value = "/find",method = RequestMethod.GET)
        public Object updateUser(long id){
                return userService.getById(id);
        }

}

主类上加扫描 mapper 路径

@SpringBootApplication
@MapperScan("com.example.demo.dao.mybatis")
public class DemoApplication {

        public static void main(String[] args) {
                SpringApplication.run(DemoApplication.class, args);
        }

}

启动项目 测试

spring boot + mybaits 操作数据库

标签:org   select   ring   column   can   实体类   update   interface   generate   

原文地址:https://blog.51cto.com/5013162/2403296

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!