SpringBoot整合通用Mapper
阿新 • • 發佈:2019-02-18
Mapper的作用:自動生成增刪改查的SQL語句 大大減化對單表的操作
專案啟動類:
@SpringBootApplication
@MapperScan(basePackages = {"yiche.com.mapper"})
/*
@MapperScan僅掃描業務介面包,不能掃描本地通用Mapper介面包,
否則報java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl
cannot be cast to java.lang.Class異常
*/
public class MapperApplication {
public static void main(String[] args){
SpringApplication.run(MapperApplication.class,args);
}
}
}
application.properties配置
#資料庫
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=xx
spring.datasource .driver-class-name=com.mysql.jdbc.Driver
#mybatis&&通用Mapper
#掃描實力類的所在包
mybatis.type-aliases-package=yiche.com.entity
#如果不是純註解 mapper介面對應的.xml檔案
#mybatis.mapper-locations=classpath:mapper/*.xml
#通用mapper的所在介面名稱 不只是包名
mapper.mappers=yiche.com.mappers.MyMapper
#資料庫
mapper.identity=MYSQL
#分頁外掛
#pagehelper.helperDialect=mysql
#pagehelper.reasonable=true
#pagehelper.supportMethodsArguments=true
#pagehelper.params=count=countSql
#在格式:logging.level.Mapper類的包=debug 會在控制檯打印出sql語句
logging.level.yiche.com.mapper=debug
專案目錄
mappers是通用mapper的所在包 mapper是自己mapper的所在包 mapper繼承通用mapper
通用mapper需要繼承這兩個介面
通用mapper介面
/*
Mapper介面:基本的增、刪、改、查方法
MySqlMapper:針對MySQL的額外補充介面,支援批量插入*/
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T> {
}
自己 的mapper介面
@Mapper
@Component
public interface EmployeeMapper extends MyMapper<Employee>{
//在使用通用Mapper的基礎上 下面的都是mybatis註解的方法
@Select("select * from employee")
List<Employee> getAll();
}
Controller:
@Controller
public class EmployeeController {
@Autowired
private EmployeeMapper employeeMapper;
@RequestMapping("/test")
@ResponseBody
public String test() {
System.out.println("ok");
return "ok";
}
/*新增 employee*/
@RequestMapping("/add")
@ResponseBody
public String add() {
Employee employee = new Employee("jack", 11000.0, 22);
employeeMapper.insert(employee);
return "ok";
}
/*批量插入 employee*/
@RequestMapping("/bachAdd")
@ResponseBody
public String bachAdd() {
Employee employee = new Employee("jack", 11000.0, 22);
List<Employee> list = new ArrayList<>();
Employee e = new Employee();
/*插入五條*/
for (int i = 0; i < 5; i++) {
e.setName("cici");
e.setSalary(13000.0);
e.setAge(i + 20);
list.add(e);
}
employeeMapper.insertList(list);
return "ok";
}
/*更據唯一主鍵查詢*/
@RequestMapping("/getById")
@ResponseBody
public Employee getById() {
//更據唯一主鍵 或者唯一編號的查詢只返回一個結果
Employee employee = new Employee();
employee.setId(1);
employee = employeeMapper.selectOne(employee);
return employee;
}
/*更據name查詢*/
@RequestMapping("/getByName")
@ResponseBody
public List<Employee> selectByName() {
//普通條件查詢可含有多條件 查詢name=cc age=100的employee
Employee employee = new Employee();
employee.setName("cici");
employee.setAge(100);
List<Employee> list = employeeMapper.select(employee);
return list;
}
/*更新 employee*/
@RequestMapping("/update")
@ResponseBody
public String update() {
//Selective 如果有這個值的話就更新 否則不更新
Employee employee = new Employee(1, "mm", 1.0, 1);
//根據主鍵更新
int i = employeeMapper.updateByPrimaryKeySelective(employee);
return i + "";
}
/*QBC查詢*/
@RequestMapping("/queryByExample1")
@ResponseBody
public List<Employee> queryByExample1(){
Example example=new Example(Employee.class);
Example.Criteria criteria=example.createCriteria();
//名字中含有c的員工 傳入實體類的屬性值
criteria.andLike("name","%c%");
//傳入criteria
return employeeMapper.selectByExample(example);
}
/*QBC查詢 傳入兩個criterial*/
@RequestMapping("/queryByExample2")
@ResponseBody
public List<Employee> queryByExample2(){
Example example=new Example(Employee.class);
Example.Criteria criteria=example.createCriteria();
Example.Criteria criteria2=example.createCriteria();
criteria.andLike("name","%c%");
//名字中含有c的員工 傳入實體類的屬性值
criteria2.andLike("name","%m%");
//加入第二個criterial
example.or(criteria2);
//傳入criteria
return employeeMapper.selectByExample(example);
}
/*QBC查詢 去重*/
@RequestMapping("/queryByExample3")
@ResponseBody
public List<Employee> queryByExample3(){
Example example=new Example(Employee.class);
Example.Criteria criteria=example.createCriteria();
//id 大於4的員工
criteria.andGreaterThan("id","4");
//排序
example.orderBy("name").asc();
//去重
example.setDistinct(true);
//select name,age from employee
example.selectProperties("id","name","age");
//傳入criteria
return employeeMapper.selectByExample(example);
}
/*以上是通用Mapper的方法*/
/*......................................................................*/
/*下面是mybatis自定義sql語句的方法*/
@RequestMapping("/getAll")
@ResponseBody
public List<Employee> getAll() {
/*select * from employee*/
return employeeMapper.getAll();
}
}
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>