本篇主要讲解MyBatis框架操作数据库,通过SpringBoot + MyBatis实现对数据库的操作
我们以学生表为例,表结构如下 :
使用步骤
- MyBatis起步依赖 : 完成MyBatis对象的自动配置,对象放在容器中
- pom.xml指定把src/main/java目录中的xml文件包含到classpath中
方式如下 :
在build标签中加入下面的语句
src/main/java ** @Mapper public interface StudentDao { @Select("select id,name,age from student where id = ${stuId}") Student selectById(@Param("stuId") Integer id); }
- 创建Service层对象,创建StudentService接口和其实现类,调用dao对象的方法
service接口的实现类
import org.springframework.stereotype.Service;
import org.zjb.dao.StudentDao;
import org.zjb.model.Student;
import org.zjb.service.StudentService;
import javax.annotation.Resource;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public Student queryStudent(Integer id) {
return studentDao.selectById(id);
}
}
- 创建Controller对象,访问Service
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zjb.service.StudentService;
import javax.annotation.Resource;
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("/student/query")
@ResponseBody
public String queryStudent(Integer id) {
return studentService.queryStudent(id).toString();
}
}
- 写application.properties文件
配置数据库的连接信息
application.properties
server.port=8081 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=123456
- 测试结果
@Mapper注解位置在每个dao接口之上,每个接口都需要使用这个注解,但是有时候我们的dao类太多,一个一个写很麻烦,现在就需要使用@MapperScan注解
@MapperScan的使用方式是,在主启动类上添加包扫描,如下:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "org.zjb.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
basePackages属性的值是一个数组可以为
@MapperScan(basePackages = {"org.zjb.dao", "org.zjb.Mapper"})
Mapper文件和dao接口分离
实际开发中,我们可能会将Mapper文件和dao接口分离,这样文件结构更加分明
分离后的文件结构如下 :
此时我们需要在application.properties文件中指定打包后的mapper文件的位置
需要添加如下语句 :
#指定mapper文件的位置
mybatis.mapper-locations=classpath:mapper
@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;
}
}
- 明确的在主启动类的上面,加入@EnableTransactionManager
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@MapperScan(basePackages = "org.zjb.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}



