Mybatis项目的一般架构(入门级程度)

本文基于IDEA,通过jar包的形式引入

项目一览~
在这里插入图片描述
注意mybatis只是方便数据库操作的,因此我们还需额外引入数据库驱动
下面依次介绍各个文件的功能~

  • mybatis-config
    这个是mybatis的配置文件,放到最外层。名字随意,只要在后面用InputStream读取的时候对应上即可
    内容如下,但需要注意
    • typeAliases中package标签的内容为实体类的包名,具体参考这篇文章
    • mappers中的mapper标签对应了后方介绍的每一个mapper,个人理解为注册~
    <?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>
    
        <typeAliases>
            <package name="entity"/>
        </typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="mapper/TestMapper.xml"/>
        </mappers>
    </configuration>
    

src一般用于存放源文件,下面介绍的所有文件都存在于src目录下~

  • MybatisUtil
    这个是一个工具类,因为我们一般是通过一个单例的SqlSessionFactory来创建不同的sqlSession来进行对数据库的操作的,所以把创建SqlSessionFactory的过程用单例模式封装,然后提供一个静态方法来创建sqlSession
    注意FileInputStream的参数,要和前面配置文件的名字对应

    public class MybatisUtil {
    
        private static SqlSessionFactory sqlSessionFactory;
    
        static {
            try {
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileInputStream("mybatis-config.xml"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        public static SqlSession getSession(boolean autoCommit){
            return sqlSessionFactory.openSession(autoCommit);
        }
    }
    
  • Main
    这个就是main函数,唯一需要注意的就是获取sqlSession时对于try closeable结构的使用~ 这样不用我们自己close资源

    public class Main {
        public static void main(String[] args) throws Exception {
    
            try(SqlSession sqlSession = MybatisUtil.getSession(true)){
                TestMapper mapper = sqlSession.getMapper(TestMapper.class);
                mapper.selectStudent().forEach(System.out::println);
            }
        }
    }
    	```
    
    
  • entity
    这个包中放的是实体类,每个实体类和数据库中的一张表对应,所以列名和属性名最好一一对应,还需要注意的就是对于 Alias 注解的使用~

  • mapper
    这位才是重量级啊,一般我们用一个接口定义操作,一个配置文件来配置该操作的sql语句

    TestMapper:

    public interface TestMapper {
        List<Student> selectStudent();
    }
    

    然后有一个同名配置文件TestMapper.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="mapper.TestMapper">
        <select id="selectStudent" resultType="Student">
            select * from student
        </select>
    </mapper>
    

    注意以下的对应关系

    • mapper和接口通过namespace属性对应
      注意,虽然mapper的配置文件和接口在同一个位置,namespace中还是要写mapper.TestMapper,并不能省略前面的包名,如果省略了会有如下报错:
    Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface mapper.TestMapper is not known to the MapperRegistry.
    	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
    	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:868)
    	at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:288)
    	at Main.main(Main.java:14)
    

    可以看到是在提醒我们产生了绑定异常:接口对注册表不可见

    • id对应着接口中的方法名
    • resultType对应改方法返回的结果在项目中的位置(为什么我这里写的是Student而不是entity.Student?这就是Alias注解的功劳~)
    • 定义完一个mapper后,记得去主配置文件夹中注册~
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值