免费邯郸网站建设,网页制作模板扩展名,网站开发服务器配置,全网最低价业务网站大纲 配置的修改代码的修改Main.java文件所在包下新增org.example.model包新增org.example.mapper包 单元测试 在《0基础学习Mybatis系列数据库操作框架——最小Demo》一文中#xff0c;我们用最简单的方法组织出一个Mybatis应用项目。为了后续构建更符合日常开发环境的项目我们用最简单的方法组织出一个Mybatis应用项目。为了后续构建更符合日常开发环境的项目我们对项目的目录结构做了调整并引入了单元测试组件JUnit。
配置的修改
在resources目录下将mybatis相关的配置聚合到名字叫mybatis的目录下这样会方便后续管理。因为实际开发中我们还会使用到很多其他组件的配置。如果散乱在resources这个目录下将不利于后期维护。 mybatis的配置由两部分组成
数据库连接和mapper文件路径。这个配置叫mybatis-config.xml我们把它放在config目录下。
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttps://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/testdb?useSSLtrueamp;useUnicodetrueamp;characterEncodingutf8/property nameusername valueroot/property namepassword valuefangliang//dataSource/environment/environmentsmappersmapper resourcemybatis/mapper/AllTypeMapper.xml//mappers
/configurationmapper文件。可能是多个mapper文件我们把它们放到mapper目录下。本例我们只设计了一个mapper但是含有两条SQL。 findAll用查询表中所有数据。findOne会根据传入的参数返回最多一条数据。
需要注意的是mapper的namespace它与后续我们定义的SQL映射器mapper接口的包(org.example.mapper)和名称(AllTypeMapper)组合一致。否则我们在后续的Java代码中不能创建SQL映射器对象。
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.mapper.AllTypeMapperselect idfindAll resultTypeorg.example.model.AllTypeselect * from all_type/selectselect idfindOne resultTypeorg.example.model.AllTypeselect * from all_type where info_int #{info_int}/select
/mapper代码的修改
主要修改分为两部分
Main.java文件所在包下
新增org.example.model包
用于保存SQL结果映射的Java对象类。
package org.example.model;public class AllType {public int getInfo_int() {return info_int;}public void setInfo_int(int info_int) {this.info_int info_int;}private int info_int;
}新增org.example.mapper包
用于保存SQL语句映射器类Mapper Class的接口定义Interface。这个概念我们并没有在《0基础学习Mybatis系列数据库操作框架——最小Demo》中涉及因为之前我们直接通过全限定名“AllTypeMapper.findAll”访问了SQL方法。而本文我们将使用映射机器来访问。 注意映射器接口中的findOne方法名在之前写好的SQL XML文件中看到过。后续我们还将在单元测试代码中见到它。 这个接口的定义连接了SQL XML和Java代码。
package org.example.mapper;import org.example.model.AllType;public interface AllTypeMapper {AllType findOne(int info_int);
}单元测试
和main目录对等建立相似的目录结构和包。 我们并不打算针对main下的model和mapper包做单元测试而是在单元测试中测试mybatis相关特性所以test目录下的org.example包下只有一个测试文件AllTypeTest.java。 不同于《0基础学习Mybatis系列数据库操作框架——最小Demo》中查询所有数据的写法 try (SqlSession s sqlSF.openSession()) {all s.selectList(org.example.mapper.AllTypeMapper.findAll);}for (AllType a : all) {System.out.println(a.getInfo_int());}本文要根据根据传入的参数动态修改SQL语句。注意下面的写法
通过connection的getMapper方法获取映射器类传入的是接口。通过映射器类的方法继承自映射器接口修改SQL语句并获得返回结果。 try (SqlSession s sqlSF.openSession()) {AllTypeMapper all_type_mapper s.getMapper(AllTypeMapper.class);AllType a all_type_mapper.findOne(1);System.out.println(a.getInfo_int());}完整代码如下
package org.example;import org.example.model.AllType;
import org.example.mapper.AllTypeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class AllTypeTest {private static SqlSessionFactory sqlSF;BeforeAllstatic void CreateSessionFactory() throws IOException {InputStream in Resources.getResourceAsStream(mybatis/config/mybatis-config.xml);sqlSF new SqlSessionFactoryBuilder().build(in);}Testvoid testFindAll() {ListAllType all null;try (SqlSession s sqlSF.openSession()) {all s.selectList(org.example.mapper.AllTypeMapper.findAll);} catch (Exception e) {System.out.println(e.getMessage());}for (AllType a : Objects.requireNonNull(all)) {System.out.println(a.getInfo_int());}}Testvoid testFindOne() {try (SqlSession s sqlSF.openSession()) {AllTypeMapper all_type_mapper s.getMapper(AllTypeMapper.class);AllType a all_type_mapper.findOne(1);if (a ! null) {System.out.println(a.getInfo_int());}} catch (Exception e) {System.out.println(e.getMessage());}}
}经过优化后的目录结构将有利于后续我们的设计和探索。