框架技术
主流框架
持久化与ORM
MyBatis
简介
环境搭建
导入mybatis-3.2.3.jar包(可添加源码)
MyBatis配置文件mybatis-config.xml(resources目录下)
<?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"> <!-- 通过这个配置文件完成mybatis与数据库的连接 --> <configuration> <!-- 引入 database.properties 文件--> <properties resource="database.properties"/> <!-- 配置mybatis的log实现为LOG4J --> <settings> <setting name="logImpl" value="LOG4J" /> </settings> <environments default="development"> <environment id="development"> <!--配置事务管理,采用JDBC的事务管理 --> <transactionManager type="JDBC"></transactionManager> <!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 将mapper文件加入到配置文件中 --> <mappers> <mapper resource="cn/smbms/dao/user/UserMapper.xml"/> </mappers> </configuration>
实体类User.java(cn.smbms.pojo目录下)
DAO层-SQL映射文件UserMapper.xml(cn.smbms.dao.user目录下)
<?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.smbms.dao.user.UserMapper"> <!-- 查询用户表的记录数 --> <select id="count" resultType="int"> select count(1) as count from smbms_user </select> <!-- 查询用户列表 --> <select id="getUserList" resultType="cn.smbms.pojo.user.User"> select * from smbms_user </select> </mapper
工具类
public class MyBatisUtil { private static SqlSessionFactory factory; static { try { // 1、获取mybatis-config.xml输入流 InputStream is = Resources .getResourceAsStream("mybatis-config.xml"); // 2、创建SqlSessionFactory对象,完成对配置文件的读取 factory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession openSqlSession() { return factory.openSession(); } public static void closeSqlSession(SqlSession sqlSession) { if (null != sqlSession) { sqlSession.close(); } } }
测试类
public class UserMapperTest { private Logger logger = Logger.getLogger(UserMapperTest.class); @Test public void test() { // 3、创建SqlSession SqlSession sqlSession = MyBatisUtil.openSqlSession(); // 4、调用mapper文件来对数据进行操作,必须先把mapper文件引入到mybatis-config.xml中 int count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count"); logger.debug("UserMapperTest count ----> " + count); MyBatisUtil.closeSqlSession(sqlSession); } @Test public void testGetUserList() { // 3、创建SqlSession SqlSession sqlSession = MyBatisUtil.openSqlSession(); // 4、调用mapper文件来对数据进行操作,必须先把mapper文件引入到mybatis-config.xml中 List<User> userList = sqlSession .selectList("cn.smbms.dao.user.UserMapper.getUserList"); for (User user : userList) { logger.debug("userName" + user.getUserName()); } MyBatisUtil.closeSqlSession(sqlSession); } }
添加本地dtd文件映射(提供编码提示,可添加config和mapper)
MyBatis基本要素
核心接口和类
SqlSessionFactoryBuilder
SqlSessionFactory
SqlSession
核心配置文件
配置properties元素的两种方式
settings元素
typeAliases元素
environments元素
mappers元素