Mybatis-----优化配置文件,基于注解CR
这篇主要写配置文件的优化,例如 jdbc.properties 配置文件 ,引入数据库的文件,例如driver,url,username,password 等,然后在 SqlMapConfig.xml 里面引入相对应的文件即可,可以简化配置文件, 在 SqlMapConfig.xml 用 <typeAliases> <typeAlias type="bean.Userinfo" alias="User"/> </typeAliases> 为包起别名 。然后封装 SqlSessionFactory 文件,因为这样写可以简化在测试类中每次都加载配置文件的操作。还有就是基于注解的方式 ,在接口 中写对应的增删改查的方法,然后@select ,@insert @delete @update 写对应的SQL,最后在SqlMapConfig.xml 里面添加<mappers> <mapper class="Mapper.UserinfoMapper"/> </mappers> 基本上就可以大功告成了, 最后可能出现的问题是在bean里面重写toString 方法,否则在控制台会把内存地址输出出来。
1 package TEST; 2 3 import org.apache.ibatis.session.SqlSession; 4 import org.junit.Test; 5 6 import bean.Userinfo; 7 8 import Mapper.UserinfoMapper; 9 import Tools.Util; 10 11 public class Demo { 12 13 14 //根据ID查询用户 15 @Test 16 public void findbyuserinfoID() throws Throwable{ 17 System.out.println("基于注解的方式查询ID"); 18 // TODO Auto-generated method stub 19 SqlSession sqlSession = Util.getSqlSession(); 20 21 //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 22 UserinfoMapper mapper = sqlSession.getMapper(UserinfoMapper.class); 23 //执行查询操作,将查询结果自动封装成User返回 24 Userinfo userinfo = mapper.getById(5); 25 //使用SqlSession执行完SQL之后需要关闭SqlSession 26 sqlSession.close(); 27 System.out.println(userinfo); 28 } 29 30 @Test 31 public void addUser() throws Throwable{ 32 System.out.println("基于注解的方式查询ID"); 33 // TODO Auto-generated method stub 34 SqlSession sqlSession = Util.getSqlSession(true); 35 36 //得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来 37 UserinfoMapper mapper = sqlSession.getMapper(UserinfoMapper.class); 38 //执行查询操作,将查询结果自动封装成User返回 39 Userinfo userinfo = new Userinfo(); 40 userinfo.setUname("楚云飞"); 41 userinfo.setUpass("123"); 42 userinfo.setId(3); 43 int adduser = mapper.addUser(userinfo); 44 //使用SqlSession执行完SQL之后需要关闭SqlSession 45 sqlSession.close(); 46 System.out.println(adduser); 47 } 48 49 }
1 package bean; 2 3 public class Userinfo { 4 5 private String uname; 6 7 private String upass; 8 9 public int id; 10 11 public int getId() { 12 return id; 13 } 14 15 public void setId(int id) { 16 this.id = id; 17 } 18 19 public String getUname() { 20 return uname; 21 } 22 23 public void setUname(String uname) { 24 this.uname = uname; 25 } 26 27 public String getUpass() { 28 return upass; 29 } 30 31 public void setUpass(String upass) { 32 this.upass = upass; 33 } 34 35 @Override 36 public String toString() { 37 return "User [id=" + id + ", name=" +uname + ", upass=" + upass + "]"; 38 } 39 40 }
1 package Mapper; 2 3 import org.apache.ibatis.annotations.Insert; 4 import org.apache.ibatis.annotations.Select; 5 6 import bean.Userinfo; 7 8 public interface UserinfoMapper { 9 @Select("select * from Userinfo where id=#{id}") 10 public Userinfo getById(int id); 11 @Insert("insert into userinfo(uname,upass,id) values(#{uname},#{upass},#{id})") 12 public int addUser(Userinfo userinfo); 13 }
package Tools; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Util { /** * 获取SqlSessionFactory * @return SqlSessionFactory */ public static SqlSessionFactory getSqlSessionFactory() { String resource = "SqlMapConfig.xml"; InputStream is = Util.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); return factory; } /** * 获取SqlSession * @return SqlSession */ public static SqlSession getSqlSession() { return getSqlSessionFactory().openSession(); } /** * 获取SqlSession * @param isAutoCommit * true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 * false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务 * @return SqlSession */ public static SqlSession getSqlSession(boolean isAutoCommit) { return getSqlSessionFactory().openSession(isAutoCommit); } }
jdbc.properties 配置文件
1 jdbc.driver=oracle.jdbc.driver.OracleDriver 2 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl 3 jdbc.username=scott 4 jdbc.password=tiger
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 6 <properties resource="jdbc.properties"></properties> 7 <!-- 自定义别名(如果在配置文件的过程中发现别名报错,可能是位置顺序有问题) --> 8 <typeAliases> 9 <typeAlias type="bean.Userinfo" alias="User"/> 10 </typeAliases> 11 <environments default="development"> 12 <environment id="development"> 13 <transactionManager type="JDBC" /> 14 <dataSource type="POOLED"> 15 <property name="driver" value="${jdbc.driver}"/> 16 <property name="url" value="${jdbc.url}"/> 17 <property name="username" value="${jdbc.username}" /> 18 <property name="password" value="${jdbc.password}" /> 19 </dataSource> 20 </environment> 21 </environments> 22 23 <!-- 读取Mapper包下的UserinfoMapper文件 --> 24 <mappers> 25 <mapper class="Mapper.UserinfoMapper"/> 26 </mappers> 27 </configuration>
最后,所有的文件都是经过测试的,可复制,可粘贴。接下来会写动态sql,以及关联查询等,敬请期待!