Mapper的作用:自动生成增删改查的SQL语句 大大减化对单表的操作
创建一个maven工程:1.版本写1.0.0-SNAPSHOT
2.使用war包
导入jar包(pom.xml):
4.0.0
cn.itcast.springboot
springboot-jdbc-3
1.0.0-SNAPSHOT
war
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
3.0
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
tk.mybatis
mapper-spring-boot-starter
1.1.7
mysql
mysql-connector-java
junit
junit
org.springframework.boot
spring-boot-maven-plugin
创建包结构:
注意:mapper包存放本地通用mapper接口类,mappers包存放业务的mapper接口类
项目启动类:
package cn.itcast;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
//整合通用mapper需要该注解扫描继承了通用mapper的类所在的包
//只能扫描业务的mapper所在的包,不能扫描本地通用mapper所在的包,否则会报错。
@MapperScan(basePackages={"cn.itcast.mappers"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
springboot启动类要放在最外层包下,才能扫描所有类。因为@SpringBootApplication组合注解默认扫描同级和子包下的类。
这里使用:@SpringBootApplication注解,不必多说。
@MapperScan :扫描业务的mapper接口所在的包,注意不能扫描本地通用mapper接口所在的包,否则会报错。
application.properties属性配置文件:
#配置连接池
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
#mybatis&&通用Mapper
#扫描实体类的所在包
mybatis.type-aliases-package=cn.itcast.pojo
#如果不是纯注解,并且你要使用自定义sql。 mapper接口对应的.xml文件
mybatis.mapper-locations=classpath:mapper/*.xml
#通用mapper的所在接口名称(不是业务的mapper) ,不只是包名
mapper.mappers=cn.itcast.mapper.MyMapper
#数据库
mapper.identity=MYSQL
#分页插件
#pagehelper.helperDialect=mysql
#pagehelper.reasonable=true
#pagehelper.supportMethodsArguments=true
#pagehelper.params=count=countSql
# 会在控制台打印出sql语句 ,格式:其中logging.level.你的业务的Mapper类包=debug
logging.level.cn.itcast.mappers=debug
这个属性配置文件的命名是固定的,必须叫application.properties。他是springboot的核心属性文件 ,启动时默认自动加载。
实体类User.java:
package cn.itcast.pojo;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Component
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", age=" + age + "]";
}
}
使用注解:@Component 在spring容器中定义该类
@Table 指定与数据库哪个表对应
@Id 声明主键
@GeneratedValue 设定自增方式
本地通用mapper接口类(MyMapper.java):
package cn.itcast.mapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper extends Mapper,MySqlMapper {
}
指定类型时使用泛型。
继承两个接口:
/* Mapper接口:基本的增、删、改、查方法
MySqlMapper:针对MySQL的额外补充接口,支持批量插入*/
业务的mapper接口类(UserMapper.java)
package cn.itcast.mappers;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import cn.itcast.mapper.MyMapper;
import cn.itcast.pojo.User;
@Mapper
@Component
public interface UserMapper extends MyMapper{
//获取用户名单
public List getUser() throws Exception;
//根据id删除用户
public void deleteUser(int id)throws Exception;
//新增用户
public void addUser(User user)throws Exception;
}
该类继承本地mapper接口类(MyMapper.java),使用@Mapper声明是一个mapper类,使用@Component将该类交给spring容器管理。
这里边有自定义的抽象方法,是因为我还用了自定义sql语句,如果你只用通用mapper的话,可以忽略此处和下一步的xml配置。
自定义sql语句的xml配置(不用的话,可忽略):
delete from user where id =#{id}
insert into user(id,username,age)values(#{id},#{username},#{age})
业务层service接口:
package cn.itcast.service;
import java.util.List;
import cn.itcast.pojo.User;
public interface UserService {
//显示所有用户
public ListgetUser()throws Exception;
//根据id删除用户
public void deleteUser(int id)throws Exception;
//新增用户
public void addUser(User user)throws Exception;
//通用mapper提供
public User selectByPrimaryKey(Long id);
}
业务层的service实现类:
package cn.itcast.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcast.mappers.UserMapper;
import cn.itcast.pojo.User;
@Service
public class UserServiceImp implements UserService {
@Autowired
private UserMapper userMapper;
public List getUser() throws Exception {
return userMapper.getUser();
}
public void deleteUser(int id) throws Exception {
userMapper.deleteUser(id);
}
public void addUser(User user) throws Exception {
userMapper.addUser(user);
}
//通用mapper提供
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
}
controller层:
package cn.itcast.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@Autowired
private User user;
//显示用户
@RequestMapping("list")
public List index() throws Exception {
return userService.getUser();
}
//删除用户
@RequestMapping("delete/{id}")
public String delete(@PathVariable int id) throws Exception {
userService.deleteUser(id);
return "你已经删掉了id为"+id+"的用户";
}
//增加用户
@RequestMapping("addUser")
public String addUser() throws Exception {
user.setAge(33);
user.setUsername("阿花");
userService.addUser(user);
return "增加用户";
}
//通用mapper提供,主键查询
@RequestMapping("selectByKey")
public void selectByKey(){
user = userService.selectByPrimaryKey(1L);
System.out.println(user.toString());
}
}
启动并测试