(1)使用mybatis-plus的.XML配置文件实现对数据库的CRUD操作

 0.项目基本结构和可能遇到的问题:

在下面实现过程中:遇到maven导出资源问题时:

 下面都有相应的解决办法。

1.设计数据库

CREATE TABLE `student` (
  `id` bigint NOT NULL,
  `name` varchar(32) NOT NULL,
  `pwd` varchar(512) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

2.设计数据库对应的实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    private Long id;
    private String name;
    private String pwd;
}

3.在pom.xml文件中引入相关依赖

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

4.编辑一个配置数据库的xml文件 

mybatis-config.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<!--连接数据库-->
<configuration>
    <environments default="development">
        <environment id="development">
             <transactionManager type="JDBC"/>
             <dataSource type="POOLED" >
                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"/>
                 <property name="username" value="root"/>
                 <property name="password" value="root"/>
                 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
             </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="generator/StudentMapper.xml"/>
    </mappers>
</configuration>

 这里需要注意的是:配置数据库文件中的末尾<mappers></mappers>这里面放的是操作数据库的xml文件,如果不添加,就会报一个找不到该配置文件的错。

初始化失败,找不到该配置文件。

5.持久层(dao层也就是这里的mapper层)

编辑StudentMapper接口

public interface StudentMapper {
    List<Student> selectAllStudent();
    Student selectAllStudentById(int id);
}

实现接口中的方法(回顾一下,以前实现的时候是写一个StudentMapperImpl.class实现类,在里面编辑实现的sql语句)现在我们通过xml文件来实现对应的接口。

编辑对应StudentMapper接口StudentMapper.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">
<!--namespace=绑定一个对应的Dao(Mapper)接口-->
<mapper namespace="com.example.ruoyitest.mapper.StudentMapper">
    <!--查询所有   配置     id表示方法      resultMap表示查询完毕后返回数据的一个类型-->
    <select id="selectAllStudent" resultType="com.example.ruoyitest.model.Student">
		select * from student;
	</select>
    <!--根据id查询一条数据-->
    <select id="selectAllStudentById" resultType="com.example.ruoyitest.model.Student">
        select * from student where id=#{id}
    </select>
</mapper>

6.编辑对应的mybatis使用到的工具类(主要是通过该工具类获取一个sqlSession对象,因为该对象里面有你后面使用到的增删查改方法)

/*
构建sqlSessionFactory目的是构建sqlSession
*/
public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;
    //类加载时就初始化sqlSessionFactory
    static{
        try {
            String resource= "generator/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //当sqlSessionFactory有了时,我们要使用其中的openSession()方法
    //这就可以使用一系列增删查改方法
    public static SqlSession getSqlSession(){
        SqlSession sqlSession=sqlSessionFactory.openSession();
        return sqlSession;
    }
}

7.接下来就是在pom.xml文件中,修改一下配置,否则会报一个以下的错误:

(1)直接运行会报以下的错误:

(2)修改配置文件:

<build>
        <!--<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                   <includes>
                       <include>**/*.properties</include>
                       <include>**/*.xml</include>
                   </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

8.进行测试:

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class MybatisTest {

    @Test
    public void mybatisTest() {
        //第一步:获取sqlSession对象
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        //执行sql  方式(1)   getMapper()
        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);

        //(1)查询全部
        List<Student> stus=mapper.selectAllStudent();
        for(Student stu:stus){
            System.out.println(stu);
        }
        //(2)根据id查询
        Student stu=mapper.selectAllStudentById(1);
        System.out.println(stu);
        //关闭sqlSession
        sqlSession.close();
    }
}

 9.测试结果:

因为我的数据库只有一条数据,所以说查询全部和单一查询都只有一条数据。

10.总结 

对单表的增删查改:可以使用mybatis generator或者MybatisPlus

对多表关联的增删查改:可以使用XML方式来实现

对单表只是实现几行的:可以使用注解方式实现

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值