springboot环境下通用mapper的整合,我使用的2.0.4版本
1.pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>
用到了通用mapper进行单表的方便操作
2.启动类
@SpringBootApplication
@MapperScan("com.itheima.mapper")
public class BootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(BootDemoApplication.class);
}
}
@springbootapplication代表启动类
@mapperScan里的参数是你mapper接口所在的包,springboot会自动创建改包下接口所有的实现类
配置文件,这里我是用的yml文件,配置了数据库等等基本信息
# 数据库
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/jjl
username: root
password: root
#tomcat和springmvc
server:
port: 8080
servlet:
path: "*.action"
# 日志
logging:
level:
com.itheima: debug
mybatis:
type-aliases-package: "com.itheima.pojo"
# org.springframework: debug
#mybatis
准备一个实体类
@Data
@Table(name = "student")
public class Student {
@Id
@KeySql(useGeneratedKeys = true)
private Integer id;
private String name;
@Transient
private String sex;
}
@data代表自动为该类生成get,set,tostring等方法,不过你要装插件才能用
如果不想使用可以自己提供
@talbe代表表名
@id代表改字段为主键
@keysql代表改字段是自增的
@transient代表mybatis不会把他加入生成的sql里
这里我只有id和name,加上transient注解就行了
mapper接口
public interface StudentMapper extends Mapper<Student> {
}
直接继承mapper接口,然后泛型填你的实体,就行
可以看到已经为我们提供了单表操作所需要的方法
service,这里和以前操作一模一样,就只贴实现类代码了
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> findAll() {
return studentMapper.selectAll();
}
@Transactional
public void addStudent(Student t) {
studentMapper.insertSelective(t);
}
}
我们在弄一个controller来测试一下
@Controller
public class StudentController {
@Autowired
private IStudentService studentService;
@RequestMapping("findAll.action")
@ResponseBody
public List<Student> findAll(){
return studentService.findAll();
}
@RequestMapping("addStudent.action")
@ResponseBody
public Student add(Student student){
studentService.addStudent(student);
return new Student();
}
}
到此准备工作完成,现在启动来测试
到此springboot整合通用Mapper完毕