- 访问网站及配置数据源
- @Transactional注解
- 多文件配置
- springboot整合mybatis
- 注解方式
- 根据xml文件配置
- MyBatis Plus
用idea创建了一个springboot架子(基于maven) 访问网站及配置数据源
数据源的配置在application.properties中,生成项目中有默认的,如下:
# 应用名称 spring.application.name=demo # 数据库驱动: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 数据源名称 spring.datasource.name=defaultDataSource # 数据库连接地址 spring.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTC # 数据库用户名&密码: spring.datasource.username=*** spring.datasource.password=***
web访问:需要@RestController和@RequestMapping
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private DataSource dataSource;
@RequestMapping("/dataSource")
public String testDataSource(){
System.out.println("数据源类型是:" + dataSource.getClass().getName());
return "OK";
}
}
在浏览器中输入:
结果:
@Transactional用来标识在类或类中的方法上。
标识在类上代表此类中所有非静态公共方法将应用数据库事务。
标识在方法上代表此方法将应用数据库事务;
@Transactional注解还定义了事务的隔离级别和传播行为,异常类型的配置,这些配置定义了事务如何执行,具有哪些特征
事务的隔离级别
DEFAUTL 值为-1,不执行事务
READ_UNCOMMITTED 值为1,未提交读,会产生脏读
READ_COMMITTED 值为2,读写提交,会产生不可重复读
PEATABLE_READ 值为4,可重复读,会产生幻读
SERIALIZABLE 值为8,串行化,最高级别,避免以上所有问题
例如:
package com.example.demo.service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
public class DepartmentService {
@Transactional(rollbackFor = java.lang.Exception.class,
isolation = Isolation.READ_COMMITTED,timeout = 1)
public int saveDepartment(){
return 0;
}
}
在psvm中要有EnableTransactionManagement
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//@SpringBootApplication
@SpringBootApplication
@EnableTransactionManagement
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意:这个注解和org.springframework的spring-tx有关,要引入
多文件配置参考这篇
文件目录:
在application.properties要加入
spring.profiles.active=dev
application-dev.yml:(修改了端口号)
server: port: 8912
结果:
使用了覆盖原有的配置的dev作为设置
属性配置文件
#配置mybatis属性 mybatis.type-aliases-package=com.example.demo
方法的接口:
package com.example.demo.mappers;
import com.example.demo.domin.Department;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface DepartmentMapper {
@Select("select * from department")
List findAll();
}
Service层
package com.example.demo.service;
import com.example.demo.domin.Department;
import com.example.demo.mappers.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class DepartmentServiceImp implements DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
@Transactional(rollbackFor = java.lang.Exception.class,
isolation = Isolation.READ_COMMITTED,timeout = 1)
public int saveDepartment(){
return 0;
}
@Override
public List findAll() {
return departmentMapper.findAll();
}
}
Controller层:
package com.example.demo.controller;
import com.example.demo.domin.Department;
import com.example.demo.service.DepartmentServiceImp;
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
@RequestMapping("/test")
public class DepartmentController {
@Autowired
private DepartmentServiceImp departmentServiceImp;
@RequestMapping("/findAll")
public String findAll(){
List departmentList = departmentServiceImp.findAll();
System.out.println(departmentList.size());
return "ok";
}
}
来访问一下:
结果略
一些注意的坑:
遇到:
这里的情况
需要在启动文件下加上
@EnableTransactionManagement(proxyTargetClass = true)
还有,配置数据库连接地址,是spring.datasource.jdbc-url,如果有问题,可能是写成了spring.datasource.url
最后,有一下错误,
java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
根据这篇文章来搞:
这篇文章
xml:
select * from emp
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//@SpringBootApplication
@SpringBootApplication
//@EnableTransactionManagement
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(value = {"com.example.demo.mappers","com.example.demo.*.mappers"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.mappers;
import com.example.demo.domin.Department;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface DepartmentMapper {
@Select("select * from department")
List findAll();
}
package com.example.demo.emp.service;
import com.example.demo.domin.Emp;
import com.example.demo.emp.mappers.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImp implements EmpService{
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List findEmp() {
return employeeMapper.findEmp();
}
}
package com.example.demo.emp.Controller;
import com.example.demo.domin.Emp;
import com.example.demo.emp.service.EmpService;
import com.example.demo.emp.service.EmpServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpService empServiceImp;
@RequestMapping("/findEmp")
public ModelAndView findEmp(){
List list = empServiceImp.findEmp();
System.out.println(list.size());
ModelAndView view = new ModelAndView("com/example/demo/page/index.jsp");
return view;
}
}
MyBatis Plus
简称MP,是myBatis的一个功能增强组件,唯一目的简化sql代码编写,提高效率,提供更多可以选择的简便操作数据API,它不对myBatis做任何更改,springboot提供了对MP的方便整合
配置:在pow加入
com.baomidou mybatis-plus mybatis-plus-latest-version
配置lombok
配置lombok后,可以在类中不用生成set和get方法,以及一些其它方法
pow.xml
org.projectlombok lombok 1.16.10



