springboot入門_數據庫訪問_mybatis
阿新 • • 發佈:2018-09-26
web local interface over date data ctype ava myba
本文記錄在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