数据库JDBC封装框架
持久层框架
https://mybatis.org/mybatis-3/zh/index.html
import cn.prelu.pojo.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select( "SELECT * FROM tb_user Where username = #{username} and password = #{password};")
User select(@Param("username") String username, @Param("password") String password);
}
定义的Interface会被Mybatis继承使用<br />写一行执行逻辑
3. 将sql的执行逻辑和参数写入接口的方法里
3. 在同级目录下完成configxml的编写
```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>
<!--起别名-->
<typeAliases>
<package name="cn.prelu.pojo"/>
</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:///db1?useSSL=false&useServerPrepStmts=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="asd"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--扫描mapper-->
<package name="cn.prelu.mapper"/>
</mappers>
</configuration>
注:如果在idea中可以使用mybatisX来简化撰写流程
- 设置sql的映射文件的namespace属性为Mapper接口全限定名
- 在Mapper里面保持参数和类型一致
-
调用MyBatis
注册配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
使用sqlsession资源读取配置文件进行调用
其中需要指定流的目标是配置文件,使用工厂案例即可获取资源打开事务
SqlSession sqlSession = sqlSessionFactory.openSession();
接口对象获取事务的sql并执行
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
注册对象资源并传参
User user = userMapper.select(username,password);
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
进行业务逻辑
if(user != null){
writer.write("LoginSuccess");
}else{
writer.write("登录失败");
}
MyBatisX
Idea插件可以在Mapper配置文件和接口中进行跳转
- 快速编写sql片段
例如下面的namespace指定了代理所需要的sql语句,但是sql通过注解完成
<?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="cn.prelu.mapper.UserMapper">
</mapper>
跳转接口定义如下:
public interface UserMapper {
@Select( "SELECT * FROM tb_user Where username = #{username} and password = #{password};")
User select(@Param("username") String username, @Param("password") String password);
}
完整的业务逻辑
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ,NullPointerException{
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.select(username,password);
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
if(user != null){
writer.write("LoginSuccess");
}else{
writer.write("登录失败");
}
}
线程池优化
若每次调用都使用SQLSessionFactroy进行创建对象将会增加大量无用线程池的内存开销
解决方案:
- 重复代码使用单例模式的静态方法进行工具类封装
- 工厂模式创建线程池对象使用静态代码块调用,各个线程和模块之间使用同一内存区域的代码
工具类的撰写:
```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 {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
实现方法代码:
```java
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();