整合Mybatis
数据库
表单
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" th:action="@{/city}">
NAME:<input type="text" name="name"><br/>W
stata:<input type="text" name="stata"><br/>
country:<input type="text" name="country"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
一、注解版
1)pom.xml引入mybatis-starter
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2)创建实体类
package com.atguigu.admin.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author LunarYouI
* @create 2021-04-14 19:12
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class City {
private Long id;
private String name;
private String stata;
private String country;
}
3)yaml配置文件链接数据库
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_test1
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#查询超时(3秒内没有查询出来就被认为查询超时) 单位:秒
jdbc:
template:
query-timeout: 3
4)mapper
package com.atguigu.admin.mapper;
import com.atguigu.admin.bean.City;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
/**
* @author LunarYouI
* @create 2021-04-14 19:14
*/
@Mapper
public interface CityMapper {
//纯注解版
@Select("select * from city where id=#{id}")
public City getById(Long id);
@Insert("insert into city(name,stata,country) value(#{name},#{stata},#{country})")
@Options(useGeneratedKeys = true,keyProperty = "id")
public void insert(City city);
}
5)service
package com.atguigu.admin.service;
import com.atguigu.admin.mapper.CityMapper;
import com.atguigu.admin.bean.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author LunarYouI
* @create 2021-04-14 19:20
*/
@Service
public class CityService {
@Autowired
CityMapper cityMapper;
public City getById(Long id){
City byId = cityMapper.getById(id);
return byId;
}
public void saveCity(City city) {
cityMapper.insert(city);
}
}
6)controller
package com.atguigu.admin.controller;
import com.atguigu.admin.bean.City;
import com.atguigu.admin.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author LunarYouI
* @create 2021-04-14 19:17
*/
@Controller
public class CityByOperation {
@Autowired
CityService cityService;
@GetMapping("biaodan")
public String biaodan(){
return "biaodan";
}
@ResponseBody
@GetMapping("/city")
public City getCityById(@RequestParam("id") Long id){
City byId = cityService.getById(id);
return byId;
}
@ResponseBody
@PostMapping("/city")
public City saveCity(City city){
cityService.saveCity(city);
return city;
}
}
7)运行结果
查询
插入
二、混合版
与注解版相差不大,唯一改动的地方接就是mapper
1)mapper
package com.atguigu.admin.mapper;
import com.atguigu.admin.bean.City;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
/**
* @author LunarYouI
* @create 2021-04-14 19:14
*/
@Mapper
public interface CityMapper {
//纯注解版
@Select("select * from city where id=#{id}")
public City getById(Long id);
// @Insert("insert into city(name,stata,country) value(#{name},#{stata},#{country})")
// @Options(useGeneratedKeys = true,keyProperty = "id")
// public void insert(City city);
//混合版
public void insert(City city);
}
2)创建mapper.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.atguigu.admin.mapper.CityMapper">
<!--只要插入数据,那么会默认的把自增主键的id返回——————使用自增的主键useGeneratedKeys="true" 自增主键的属性是什么keyProperty-->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into city(name,stata,country) value(#{name},#{stata},#{country})
</insert>
</mapper>
3)配置文件
#JavaBean位置
mybatis.type-aliases-package=com.atguigu.admin.bean
#mapper.xml位置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
如果不愿意每次都往mapper下的**mapper接口中添加@mapper注解 那么可以在启动类中添加@MapperScan(“com.atguigu.admin.mapper”)