使用mybatis框架操作数据,在springboot框架中集成mybatis
使用步骤:
mybatis起步依赖:完成mybatis对象自动配置,对象放在容器中。
org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test
pom.xml指定把src/main/java目录中的xml文件包含到classpath中。
src/main/java **/*.xml org.springframework.boot spring-boot-maven-plugin
创建实体类Studnet
创建Dao接口StudentDao,创建一个查询学生的方法。
/**
* @Mapper :告诉MyBatis这是dao接口,创建此接口的代理对象,
* 位置:在类的上面。
* **/
@Mapper
public interface StudentDao {
Student selectById(@Param("stuId") Integer id);
}创建Dao接口对应的Mapper文件,xml文件,写sql语句。
/**
* @Mapper :告诉MyBatis这是dao接口,创建此接口的代理对象,
* 位置:在类的上面。
* **/
@Mapper
public interface StudentDao {
Student selectById(@Param("stuId") Integer id);
}创建servlet层对象,创建StudentService接口和它的实现类。去调用dao对象的方法,完成数据库的操作。
package com.firewolf.service;
public interface StudentService {
Student queryStudent(Integer id);
}
package com.firewolf.service.impl;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public Student queryStudent(Integer id) {
Student student=studentDao.selectById(id);
return student;
}
}创建Controller对象,访问Service。
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("/student/query")
@ResponseBody
public String queryStudent(Integer id){
Student student = studentService.queryStudent(id);
return student.toString();
}
}写application.properties文件。
配置数据库的连接信息
server.port=9001 server.servlet.context-path=/orm # 连接数据库 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=991231gao
1.第一种方式:@Mapper
@Mapper:放在dao接口的上面,每个接口都需要使用这个注解。
/**
* @Mapper :告诉MyBatis这是dao接口,创建此接口的代理对象,
* 位置:在类的上面。
* **/
@Mapper
public interface StudentDao {
Student selectById(@Param("stuId") Integer id);
}2.第二种方式 @MapperScan
/**
* @MapperScan : 找到Dao接口和Mapper文件。
* basePackages:dao接口所在的包名
* **/
@SpringBootApplication
@MapperScan(basePackages = {"com.firewolf.dao","com.firewolf.mapper"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}3.第三种方式:Mapper文件和Dao接口分开管理
现在把Mapper文件放在resources
在resources目录中创建子目录,例如mapper
把mapper文件放到mapper目录中。
在application.properties文件中,指定mapper文件的目录。
# 指定mapper文件的位置 mybatis.mapper-locations=classpath:mapper/*.xml # mybaitis的日志 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
在pom.xml中指定目录,把resources目录中的文件,编译到目标目录中。
src/main/resources **/*.*
4.事务
spring框架中的事务
管理事务的对象:事务管理器(接口,接口有很多的实现类)。
例如:使用jdbc或mybatis访问数据库,使用事务管理器:DataSourceTransactionManager
声明式事务:在xml配置文件或者使用注解说明事务控制的内容。
控制事务:隔离级别,传播行为,超时时间。
事务处理方式
spring框架中的@Transactional
aspectj框架可以在xml配置文件中,声明事务控制的内容。
springboot中使用事务:上面的两种方式都可以。
在业务方法的上面加入@Transactional,加入注解后,方法有事务功能了。
明确在主启动类的上面,加入@EnableTransactionManager。
@SpringBootApplication
@EnableTransactionManagement
@MapperScan(value="com.firewolf.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}例子:
/**
* @Transactional: 表示方法的有事务支持
* 默认:使用库的隔离级别, REQUIRED 传播行为; 超时时间 -1
* 抛出运行时异常,回滚事务
*/
@Transactional
@Override
public int addStudent(Student student) {
System.out.println("业务方法addStudent");
int rows = studentDao.insert(student);
System.out.println("执行sql语句");
//抛出一个运行时异常, 目的是回滚事务
//int m = 10 / 0 ;
return rows;
}