栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ORM操作MySQL

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ORM操作MySQL

ORM操作MySQL

本篇主要讲解MyBatis框架操作数据库,通过SpringBoot + MyBatis实现对数据库的操作

我们以学生表为例,表结构如下 :

mapper文件的方式

使用步骤

  1. MyBatis起步依赖 : 完成MyBatis对象的自动配置,对象放在容器中
  2. 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);
}
  1. 创建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);
    }
}
  1. 创建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();
    }
}
  1. 写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
  1. 测试结果
@Mapper和@MapperScan

@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;
    }
}
  1. 明确的在主启动类的上面,加入@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);
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659399.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号