MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。它避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。因为 MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs (Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录。
项目创建
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
实体类代码
package com.example.demo.beans;
public class Customer {
private int id;
private String cid;
private String xm;
private String lxdh;
private String ctime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getLxdh() {
return lxdh;
}
public void setLxdh(String lxdh) {
this.lxdh = lxdh;
}
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
@Override
public String toString() {
return "Customer{" +
"id="+id +
",cid="+cid +
",xm="+xm +
",lxdh="+lxdh +
",ctime="+ctime +
"}";
}
}
controller层代码
package com.example.demo.controller;
import com.example.demo.beans.Customer;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@RestController
@RequestMapping("/")
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
*3、占位符映射
* 语法:@RequestMapping(value=”user/{userId}/{userName}”)
* 请求路径:http://localhost:8080/pathvariable/1/java
* @param ids
* @param names
* @return
*/
@RequestMapping("/pathvariable/{id}/{name}")
public ModelAndView test5(@PathVariable("id") Long ids , @PathVariable("name") String names){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","占位符映射:id:"+ids+";name:"+names);
mv.setViewName("pathvariable");
return mv;
}
@RequestMapping("/getCustomer")
public @ResponseBody Customer getCustomer(@RequestParam int id)throws Exception {
try{
return customerService.getCustomer(id);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
代码说明:@RestController 这个注解等效于 @Controller加上 @ResponseBody,添加了这个注解就是让这个类返回json串,这是spring内部提供的json解析。@RequesMapping 注解是一个地址映射的注解。就是根据这个地址,可以找到这个方法,这个类,注解到类上,就相当于方法的父类地址
@PathVariable 获取url中的数据,通过@PathVariable注解来获取URL中的参数时的前提条件是我们知道url的格式时怎么样的
service层代码
package com.example.demo.service.impl;
import com.example.demo.beans.Customer;
import com.example.demo.dao.CustomerDao;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service (value = "customerService")
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
public Customer getCustomer(int id )throws Exception{
return customerDao.SelObj(id);
}
}
dao层代码
package com.example.demo.dao;
import com.example.demo.beans.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface CustomerDao {
public Customer SelObj(int id);
}
SelObj 方法对应mapping xml文件中的id
不需要mapping xml文件的话,就在方法上加上@select
//不需要mapping配置文件也是可以的 @Select("select * from tb_customer where id = #{id}") public Customer SelObj(int id);
mapper层代码
<?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.CustomerDao">
<resultMap id="customer" type="com.example.demo.beans.Customer">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="xm" jdbcType="VARCHAR" property="xm" />
<result column="lxdh" jdbcType="VARCHAR" property="lxdh" />
<result column="ctime" jdbcType="VARCHAR" property="ctime" />
</resultMap>
<select id="SelObj" resultType="com.example.demo.beans.Customer">
select * from tb_customer where id = #{id}
</select>
</mapper>
namespace需要与dao类对应,不然会报异常。