springboot入门_数据库访问_mybatis

Posted wlzq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot入门_数据库访问_mybatis相关的知识,希望对你有一定的参考价值。

本文记录在springboot中使用mybatis访问数据库。

创建项目并引入对应的依赖

 1 <dependency>
 2             <groupId>org.springframework.boot</groupId>
 3             <artifactId>spring-boot-starter-web</artifactId>
 4         </dependency>
 5         <dependency>
 6             <groupId>org.mybatis.spring.boot</groupId>
 7             <artifactId>mybatis-spring-boot-starter</artifactId>
 8             <version>1.3.2</version>
 9         </dependency>
10         <dependency>
11             <groupId>mysql</groupId>
12             <artifactId>mysql-connector-java</artifactId>
13             <scope>runtime</scope>
14         </dependency>

在application.properties文件中配置数据库链接信息

1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2 spring.datasource.url=jdbc:mysql://localhost:3306/test
3 spring.datasource.username=root
4 spring.datasource.password=123456

创建实体类,与数据库表字段做映射

 1 public class City {
 2 
 3     private Long id;
 4     private String cityCode;
 5     private String cityName;
 6     //此处省略get和set方法
 7 
 8     @Override
 9     public String toString(){
10         return "{id:"+this.getId()+",cityName:"+this.getCityName()+",cityCode:"+this.getCityCode()+"}";
11     }
12 
13 }

创建dao接口类。mybatis访问数据库有两种方式,一种是使用annotation注解,一种是使用xml配置文件

1 使用annotation接口,可以在注解上直接写SQL访问数据库

 1 @Mapper
 2 public interface CityMapper {
 3 
 4     @Insert("insert into t_city(cityCode, cityName) values(#{cityCode},#{cityName})")
 5     int insert(City city);
 6 
 7     @Delete("delete from t_city where id = #{id}")
 8     int delete(Integer id);
 9 
10     @Update("update t_city set cityName = #{cityName} where cityCode = #{cityCode}")
11     int update(City city);
12 
13     @Select("select * from t_city where id = #{id}")
14     City selectByPrimaryKey(@Param("id") Integer id);
15 
16 }

2 使用xml配置文件,这种方式需要在application.properties中指定xml文件所在路径 

mybatis.mapper-locations=classpath:mybatis/*.xml

定义接口

 1 @Mapper
 2 public interface CityMapper {
 3 
 4     int insert(City city);
 5 
 6     int delete1(Integer id);
 7 
 8     int update1(City city);
 9 
10     City selectByPrimaryKey1(@Param("id") Integer id);
11 
12 }

接口对应的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="org.allen.learn.dao.City1Mapper" >
    <resultMap id="BaseResultMap" type="org.allen.learn.domain.City" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="cityCode" property="cityCode" jdbcType="VARCHAR" />
        <result column="cityName" property="cityName" jdbcType="VARCHAR" />
    </resultMap>

    <select id="selectByPrimaryKey1" resultMap="BaseResultMap">
      select * from t_city where id = #{id}
    </select>

    <update id="update1" parameterType="org.allen.learn.domain.City">
      update t_city set cityName = #{cityName} where cityCode = #{cityCode}
    </update>

    <insert id="insert" parameterType="org.allen.learn.domain.City">
      insert into t_city(cityCode, cityName) values (#{cityCode},#{cityName})
    </insert>

    <delete id="delete1" parameterType="java.lang.Integer">
        delete from t_city where id = #{id}
    </delete>
</mapper>

创建service类

 1 @Service
 2 public class CityService implements CityMapper {
 3 
 4     @Autowired
 5     private CityMapper cityMapper;
 6 
 7     @Override
 8     public int insert(City city) {
 9         return cityMapper.insert(city);
10     }
11 
12     @Override
13     public int delete(Integer id) {
14         return cityMapper.delete(id);
15     }
16 
17     @Override
18     public int update(City city) {
19         return cityMapper.update(city);
20     }
21 
22     @Override
23     public City selectByPrimaryKey(Integer id) {
24         return cityMapper.selectByPrimaryKey(id);
25     }
26 }

最后写一个controller,做测试

 1 @RestController
 2 @RequestMapping("data/city/mybatis")
 3 public class CityController {
 4 
 5     @Autowired
 6     private CityService cityService;
 7 
 8     @PutMapping
 9     public String save(@RequestBody City city){
10         cityService.insert(city);
11         return "success";
12     }
13 
14     @DeleteMapping("/{id}")
15     public String delete(@PathVariable Integer id){
16         cityService.delete(id);
17         return "success";
18     }
19 
20     @PostMapping
21     public String update(@RequestBody City city){
22         cityService.update(city);
23         return "success";
24     }
25 
26     @GetMapping("/{id}")
27     public City findById(@PathVariable Integer id){
28         City city = cityService.selectByPrimaryKey(id);
29         return city;
30     }
31 
32 }

使用postman工具做接口测试。

 

以上是关于springboot入门_数据库访问_mybatis的主要内容,如果未能解决你的问题,请参考以下文章

springboot_数据访问之mybatis整合

SpringBoot_入门

springboot入门_数据库访问_jdbcTemplate

springboot入门_数据库访问_jpa

SpringBoot + Mybatis-puls + ClickHouse增删改查入门教程

mybatis入门_mybatis基本原理以及入门程序