springBoot 集成 Mybatis,linux 系统编程手册 pdf 百度云
spring.datasource.username = root
spring.datasource.password = Root@123
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
3.创建持久化 bean
=================
package com.huawei.beans;
public class Student {
private Integer id;
private String name;
public Student() {
}
public Student(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" + "id=" + id + ", name='" + name + ''' + '}';
}
}
4.创建 dao
=============
package com.huawei.mapper;
import com.huawei.beans.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
@Select("select * from student where name = #{name}")
public List<Student> likeName(String name);
@Select("select *from student where id = #{id}")
public Student getById(long id);
@Select("select name from student where id = #{id}")
public String getNameById(long id);
}
5.创建 service
=================
package com.huawei.service;
import com.huawei.beans.Student;
import com.huawei.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> likeName(String name) {
return studentMapper.likeName(name);
}
}
_**6.创建 cont
roller**_
====================
package com.huawei.controller;
import com.github.pagehelper.PageHelper;
import com.huawei.beans.Student;
import com.huawei.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/likeName")
public List<Student> likeName(String name){
PageHelper.startPage(1,2);
return studentService.likeName(name);
}
}
7.创建分页配置 bean
==================
package com.huawei.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class MyBatisConfiguration {
@Bean
public PageHelper pageHelper() {
System.out.println("MyBatisConfiguration.pageHelper()");
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
8.创建启动类
=============
package com.huawei;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.huawei.*")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
运行:
=========
问题:
=========
分页版本是 4.2.0 时,会导致第一次分页成功,第二层抛出异常的问题,使用 4.2.1 就可以解决问题
9.使用 insert 获取自增长 ID
=======================
在 mapper 中添加 insert 方法
@Insert("insert into student(name) values(#{name})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
public void save(Student student);
添加 service
评论