优化配置
1.提取properties配置文件
提取jdbc.properties配置文件 Mybatis-config.xml依然采用${driverName}的方式来引用
通过properties建立两者之间的联系
|
直接上代码:
1.建个项目:
2.建包,建配置文件
3.con.qf.entity包中创建实体类User:
package com.qf.entity; public class User { private Integer id; private String name; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }
4.con.qf.dao包中创建接口
package com.qf.dao; import java.util.List; import com.qf.entity.User; public interface IUserDao { public User getUserById(Integer id); public void add(User user); public void delete(Integer id); public void update(User user); public List<User> getUserList(); }
5.com.qf.mapper包创建接口的映射文件
<?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"> <!-- namespace指的就是接口的全类名 --> <mapper namespace="com.qf.dao.IUserDao"> <!-- id:方法名称 resultType:返回类型 --> <select id="getUserById" resultType="com.qf.entity.User"> select * from t_user where id = #{id} </select> <!-- parameterType:方法的参数类型 --> <insert id="add" parameterType="com.qf.entity.User"> insert into t_user (name,password) values(#{name},#{password}) </insert> <update id="update"> update t_user set name = #{name},password = #{password} where id = #{id} </update> <delete id="delete" parameterType="java.lang.Integer"> delete from t_user where id = #{id} </delete> <!-- 方法返回集合的时候resultType要配置集合中的类型 --> <select id="getUserList" resultType="com.qf.entity.User"> select * from t_user </select> </mapper>
6.com.qf.util工具类
package com.qf.utils; import java.io.IOException; import java.io.InputStream; 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.xml.sax.InputSource; public class MyBatisUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() { if (sqlSessionFactory == null) { initSqlSessionFactory(); // 为空再去参加 } return sqlSessionFactory; } private static void initSqlSessionFactory() { InputStream ips = null; try { // 1.指定配置文件 ips = Resources.getResourceAsStream("mybatis-config.xml"); // 2.赋值操作 synchronized (MyBatisUtil.class) { if (sqlSessionFactory == null) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips); } } } catch (IOException e) { e.printStackTrace(); } finally { if (ips != null) { try { ips.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
7.配置mybatis-config.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> <!-- 指定使用哪个environment --> <environments default="development"> <environment id="development"> <!-- 事务管理器 --> <transactionManager type="JDBC" /> <!-- 连接池,POOLED是MyBatis提供的链接池 --> <dataSource type="POOLED"> <!-- 链接数据库的参数 --> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/1707_hibernate" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- Mapper文件 --> <mappers> <mapper resource="com/qf/mapper/IUserDao.xml" /> </mappers> </configuration>
8.配置jdbc.properties配置文件
jdbc.url =jdbc:mysql://localhost:3306/1707_hibernate jdbc.name=root jdbc.password=root jdbc.driver=com.mysql.jdbc.Driver
9.到mybatis-config.xml里面去配置读取jdbc.properties文件
<?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> <!-- 加载属性文件 resource:加载本地资源文件 url:远程的资源 如果调用远程的接口,返回的是key-value形式信息,通过key去取值 --> <properties resource="jdbc.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.name}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <!-- Mapper文件 --> <mappers> <mapper resource="com/qf/mapper/IUserDao.xml" /> </mappers> </configuration>
测试类:
package com.qf.dao; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.log4j.Logger; import org.junit.Test; import com.qf.entity.User; import com.qf.utils.MyBatisUtil; public class UserDaoTest { private Logger logger = Logger.getLogger(UserDaoTest.class); @Test public void testGetUserById() { SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); User user = userDao.getUserById(9); sqlSession.close(); } }
总结:
1.属性文件的读取
1)加载属性文件
a)<properties resource="jdbc.properties">
2)通过$(属性文件中的key)符号获取值