数据库JDBC封装框架
持久层框架
https://mybatis.org/mybatis-3/zh/index.html

  1. 安装,导入依赖
  2. 进行代理设置

    Mapper代理

  3. 提前定义好业务逻辑所需的对象,例如User

  4. 定义针对该对象的接口 ```java package cn.prelu.mapper;

import cn.prelu.pojo.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select;

public interface UserMapper {

  1. @Select( "SELECT * FROM tb_user Where username = #{username} and password = #{password};")
  2. User select(@Param("username") String username, @Param("password") String password);

}

  1. 定义的Interface会被Mybatis继承使用<br />写一行执行逻辑
  2. 3. sql的执行逻辑和参数写入接口的方法里
  3. 3. 在同级目录下完成configxml的编写
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8" ?>
  6. <!DOCTYPE configuration
  7. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  8. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  9. <configuration>
  10. <!--起别名-->
  11. <typeAliases>
  12. <package name="cn.prelu.pojo"/>
  13. </typeAliases>
  14. <environments default="development">
  15. <environment id="development">
  16. <transactionManager type="JDBC"/>
  17. <dataSource type="POOLED">
  18. <property name="driver" value="com.mysql.jdbc.Driver"/>
  19. <property name="url" value="jdbc:mysql:///db1?useSSL=false&amp;useServerPrepStmts=true&amp;characterEncoding=utf-8"/>
  20. <property name="username" value="root"/>
  21. <property name="password" value="asd"/>
  22. </dataSource>
  23. </environment>
  24. </environments>
  25. <mappers>
  26. <!--扫描mapper-->
  27. <package name="cn.prelu.mapper"/>
  28. </mappers>
  29. </configuration>

注:如果在idea中可以使用mybatisX来简化撰写流程

  1. 设置sql的映射文件的namespace属性为Mapper接口全限定名
  2. 在Mapper里面保持参数和类型一致
  3. 编码撰写业务逻辑

    调用MyBatis

  4. 注册配置文件

    1. String resource = "mybatis-config.xml";
    2. InputStream inputStream = Resources.getResourceAsStream(resource);
    3. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    使用sqlsession资源读取配置文件进行调用
    其中需要指定流的目标是配置文件,使用工厂案例即可获取资源

  5. 打开事务

    1. SqlSession sqlSession = sqlSessionFactory.openSession();
  6. 接口对象获取事务的sql并执行

    1. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  7. 注册对象资源并传参

    1. User user = userMapper.select(username,password);
    2. resp.setContentType("text/html;charset=utf-8");
    3. PrintWriter writer = resp.getWriter();
  8. 进行业务逻辑

    1. if(user != null){
    2. writer.write("LoginSuccess");
    3. }else{
    4. writer.write("登录失败");
    5. }

    MyBatisX

  9. Idea插件可以在Mapper配置文件和接口中进行跳转

  10. 快速编写sql片段

例如下面的namespace指定了代理所需要的sql语句,但是sql通过注解完成

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="cn.prelu.mapper.UserMapper">
  6. </mapper>

跳转接口定义如下:

  1. public interface UserMapper {
  2. @Select( "SELECT * FROM tb_user Where username = #{username} and password = #{password};")
  3. User select(@Param("username") String username, @Param("password") String password);
  4. }

完整的业务逻辑

  1. @Override
  2. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ,NullPointerException{
  3. req.setCharacterEncoding("utf-8");
  4. String username = req.getParameter("username");
  5. String password = req.getParameter("password");
  6. System.out.println(username);
  7. System.out.println(password);
  8. String resource = "mybatis-config.xml";
  9. InputStream inputStream = Resources.getResourceAsStream(resource);
  10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  11. SqlSession sqlSession = sqlSessionFactory.openSession();
  12. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  13. User user = userMapper.select(username,password);
  14. resp.setContentType("text/html;charset=utf-8");
  15. PrintWriter writer = resp.getWriter();
  16. if(user != null){
  17. writer.write("LoginSuccess");
  18. }else{
  19. writer.write("登录失败");
  20. }
  21. }

线程池优化

若每次调用都使用SQLSessionFactroy进行创建对象将会增加大量无用线程池的内存开销
解决方案:

  1. 重复代码使用单例模式的静态方法进行工具类封装
  2. 工厂模式创建线程池对象使用静态代码块调用,各个线程和模块之间使用同一内存区域的代码

    工具类的撰写:

    ```java package cn.prelu.util;

import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException; import java.io.InputStream;

public class SqlSessionFactoryUtil {

  1. private static SqlSessionFactory sqlSessionFactory;
  2. static {
  3. String resource = "mybatis-config.xml";
  4. InputStream inputStream = null;
  5. try {
  6. inputStream = Resources.getResourceAsStream(resource);
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  11. }
  12. public static SqlSessionFactory getSqlSessionFactory(){
  13. return sqlSessionFactory;
  14. }

}

  1. 实现方法代码:
  2. ```java
  3. SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();