当前位置: 首页 > 图灵资讯 > 技术篇> ORM操作数据库

ORM操作数据库

来源:图灵教育
时间:2023-06-04 09:13:08

1.创建40个数据库

解释MyBatis框架,读写MySQL数据。数据库学生表通过SpringBot+MyBatis实现

查询操作。

参考数据库:springboot.sql脚本文件

创建数据库:数据库springboot,utf-8

ORM操作数据库_ORM操作数据库

ORM操作数据库_spring_02

插入数据

ORM操作数据库_SpringBoot_03

2.使用步骤:41

使用Mybatis框架操作数据,将Mybatis集成到Springboot框架中

2.1mybatis起步依赖:411

在容器中完成mybatis对象的自动配置

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.7.11</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.bjpowernode</groupId>    <artifactId>course8</artifactId>    <version>0.0.1-SNAPSHOT</version>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies><!--        起步依赖web-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency><!--        起步依赖于mybatis-->        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>2.3.0</version>        </dependency><!--        mysql驱动-->        <dependency>            <groupId>com.mysql</groupId>            <artifactId>mysql-connector-j</artifactId>            <scope>runtime</scope>        </dependency><!--        测试-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

2.2pom.指定src///xmlmainxml文件在java目录中包含在classpath中的45pomm.在xml文件中修改

<!--resources插件 45            这个插件的作用是使用src/main/java目录下的所有内容.包含xml文件 target中            在这里,我们将Studentdaoo.xml 包含mybatissqql映射文件-->        <resources>            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>            </resource>        </resources>

2.3创建实体Student42

package com.bjpowernode.model;//实体类  42public class Student {    private Integer id;    private String name;    private Integer age;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    @Override    public String toString() {        return "Student{" +                "id=" + id +                ", name='" + name + '\'' +                ", age=" + age +                '}';    }}

2.4创建Dao接口StudentDao,创建查询学生的方法42持久层接口StudentDao

package com.bjpowernode.dao;import com.bjpowernode.model.Student;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;///持久层接口Studentent  42@Mapper ///告诉Mybatis这是dao接口,创建此接口的代理对象public interface StudentDao {    Student selectById(@Param("stuId") Integer id);//@Param是id的别名

2.5创建与Dao界面对应的Mapper文件,xml文件,写sql语句42

sql映射文件mybatis

StudentDao.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.bjpowernode.dao.StudentDao"><!--    sql映射文件mybatis   42-->    <!--定义sql语句  -->    <select id="selectById" resultType="com.bjpowernode.model.Student">        select id,name,age from student where id=#{stuId}    </select></mapper>

2.6创建Service层对象433

创建StudentService接口及其实现类。去dao对象的方法。完成数据库的操作

Studentservicent业务成接口Studentser

package com.bjpowernode.service;import com.bjpowernode.model.Student;///业务层接口  43public interface StudentService {    Student queryStudent(Integer id);}

业务层接口实现StudentServiceimplimple

package com.bjpowernode.service.impl;import com.bjpowernode.dao.StudentDao;import com.bjpowernode.model.Student;import com.bjpowernode.service.StudentService;import org.springframework.stereotype.Service;import javax.annotation.Resource;/////业务层家口实现类  43@Servicepublic class StudentServiceImpl implements StudentService {    @Resource///自动注入    private StudentDao studentDao;    @Override    public Student queryStudent(Integer id) {        Student student = studentDao.selectById(id);        return student;    }}

2.7创建Controller对象,访问Service。表示层Studentcontroler。

package com.bjpowernode.controller;import com.bjpowernode.model.Student;import com.bjpowernode.service.StudentService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;//表示层  44@Controllerpublic class StudentController {    @Resource    private StudentService studentService;        @RequestMapping("/student/query")    @ResponseBody ///意味着返回结果是数据    public String queryStudent(Integer id){        Student student = studentService.queryStudent(id);        return student.toString();    }}

2.8写application.properties文件45

配置数据库的连接信息。

application.properties

server.port=9001server.servlet.context-path=/orm#连接数据库   45spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.password=lzl

项目包的布局

ORM操作数据库_xml_04

3.第一种方式:@Mapper47

@Mapper:在dao接口上,每个接口都需要使用此注释。

告诉Mybatis这是dao接口,创建此接口的代理对象

Studentdaontdao

package com.bjpowernode.dao;import com.bjpowernode.model.Student;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;///持久层接口Studentent  42@Mapper ///告诉Mybatis这是dao接口,创建此接口的代理对象public interface StudentDao {    Student selectById(@Param("stuId") Integer id);//@Param是id的别名

测试46

输入浏览器http://localhost:9001/orm/student/query?id=1.即查询ID=1的学生

ORM操作数据库_xml_05

4.@MapperScan48

course8_1代码

添加在Dao接口上@Mapper,注释需要添加到每个接口中。Dao接口多的时候不方便。

可以用以下方法解决。

注解包扫描添加到主类:@MapperScan("com.bjpowernode.dao")

修改位置的项目代码与上述程序相同:

1.去掉Studentdao界面上的@Mapper注释

2.加入主类@MapperScan()

StudentDao

package com.bjpowernode.dao;import com.bjpowernode.model.Student;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;///持久层接口Studentent  48//@MapperScan 注解的使用  48public interface StudentDao {    Student selectById(@Param("stuId") Integer id);//@Param是id的别名

Course8Application

package com.bjpowernode;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @MapperScan: 找到Dao接口和Mapper文件  48 *     basePackages:Dao接口所在的包名 */@SpringBootApplication@MapperScan(basePackages = {"com.bjpowernode.dao","com.bjpowernode.mapper"})public class Course8Application {    public static void main(String[] args) {        SpringApplication.run(Course8Application.class, args);    }}

输入浏览器http://localhost:9001/orm/student/query?id=2

ORM操作数据库_java_06

5.49分别管理mapper文件和java代码

建议将mapper文件放在resources目录下,将java代码放在src/main/java。

实现步骤:

➢在resources创建自定义目录,如mapper,存储xml文件

ORM操作数据库_xml_07

➢将原来的xml文件剪切并复制到resources/mapper目录

➢application.在properties配置文件中指定映射文件的位置,该配置只有接口和映射