mybatis之註解方式實現
阿新 • • 發佈:2018-01-15
nsa pub oct cto fault map bsp val neu
* 使用mybatis舉例,使用註解方式實現
* 不需要針對UserMapperI接口去編寫具體的實現類代碼,這個具體的實現類由MyBatis幫我們動態構建出來,我們只需要直接拿來使用即可。
* 1、導入jar包:mybatis和mysql-connector
* 2、mybatis配置文件:mybatis-config.xml,加載Mapper接口路徑
* 3、編寫JavaBean類:UserBean
* 4、編寫執行sql接口
* 5、編寫測試類進行測試
mybatis-config-zhujie.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2<!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!--從外部配置文件導入jdbc信息--> 7 <properties resource="jdbc.properties"></properties> 8 9 <environments default="development"> 10 <environment id="development"> 11 <transactionManager type="JDBC"/> 12 <dataSource type="POOLED"> 13 <property name="driver" value="${driver}"/> 14 <property name="url" value="${url}"/> 15<property name="username" value="${username}"/> 16 <property name="password" value="${password}"/> 17 </dataSource> 18 </environment> 19 </environments> 20 21 <!--指定映射資源文件--> 22 <mappers> 23 <mapper class="zhujie.UserMapperInterface"></mapper> 24 25 </mappers> 26 </configuration>
UserMapperInterface.java
1 package zhujie; 2 3 import bean.UserSalary; 4 import first.UserBean; 5 import org.apache.ibatis.annotations.Delete; 6 import org.apache.ibatis.annotations.Insert; 7 import org.apache.ibatis.annotations.Select; 8 import org.apache.ibatis.annotations.Update; 9 10 import java.util.List; 11 12 /* 13 * 使用mybatis舉例,使用註解方式實現 14 * 不需要針對UserMapperI接口去編寫具體的實現類代碼,這個具體的實現類由MyBatis幫我們動態構建出來,我們只需要直接拿來使用即可。 15 * 1、導入jar包:mybatis和mysql-connector 16 * 2、mybatis配置文件:mybatis-config.xml,加載Mapper接口路徑 17 * 3、編寫JavaBean類:UserBean 18 * 4、編寫執行sql接口 19 * 5、編寫測試類進行測試,自動實例化,調用方法 20 * */ 21 public interface UserMapperInterface { 22 23 // UserBean 24 @Select("select * from user where id = #{id}") 25 public UserBean selectOneUser(int id); 26 27 @Select("select * from user") 28 public List<UserBean> selectAllUser(); 29 30 @Insert("insert into user (id, name, age) values (#{id} ,#{name}, #{age})") 31 public int insertUser(UserBean userBean); 32 33 @Update("update user set name=#{name} where id=#{id}") 34 public int updateUser(UserBean userBean); 35 36 @Delete("delete from user where id=#{id}") 37 public int deleteById(int id); 38 39 @Delete("delete from user where id=#{id}") 40 public int deleteByUserBean(UserBean userBean); 41 42 // 使用User和Salary表聯合查詢 43 @Select("select u.id, u.name, u.age, s.salary from user u, salary s where u.name = s.name and u.name = #{name}") 44 public UserSalary selectOneUserSalary(String name); 45 46 }
TestZhuJie.java
1 package zhujie; 2 3 import bean.UserSalary; 4 import first.UserBean; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Ignore; 12 import org.junit.Test; 13 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.util.List; 17 18 /* 19 * 使用mybatis舉例,使用註解方式實現 20 * 不需要針對UserMapperI接口去編寫具體的實現類代碼,這個具體的實現類由MyBatis幫我們動態構建出來,我們只需要直接拿來使用即可。 21 * 1、導入jar包:mybatis和mysql-connector 22 * 2、mybatis配置文件:mybatis-config.xml,加載Mapper接口路徑 23 * 3、編寫JavaBean類:UserBean 24 * 4、編寫執行sql接口 25 * 5、編寫測試類進行測試 26 * */ 27 public class TestZhuJie { 28 String resource = "mybatis-config-zhujie.xml"; 29 SqlSessionFactory sqlSessionFactory = null; 30 SqlSession session = null; 31 32 @Before 33 public void before() { 34 // System.out.println("Before"); 35 try { 36 InputStream inputStream = Resources.getResourceAsStream(resource); 37 // 創建工廠 38 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 39 // 創建session對象 40 session = sqlSessionFactory.openSession(); 41 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 47 @After 48 public void close() { 49 session.close(); 50 // System.out.println("After"); 51 } 52 53 @Test 54 public void testSelectOneUser() { 55 // 接口自動實例化 56 UserMapperInterface userMapperInterface = session.getMapper(UserMapperInterface.class); 57 // 執行sql 58 UserBean userBean = userMapperInterface.selectOneUser(1); 59 System.out.println(userBean); 60 } 61 62 // 批量查詢 63 @Test 64 public void testSelectAllUser() { 65 // 接口自動實例化 66 UserMapperInterface userMapperInterface = session.getMapper(UserMapperInterface.class); 67 // 執行sql 68 List<UserBean> listUserBean = userMapperInterface.selectAllUser(); 69 System.out.println("記錄個數:" + listUserBean.size()); 70 System.out.println(listUserBean); 71 } 72 73 @Ignore 74 @Test 75 public void testInsertUser() { 76 UserBean userBean = new UserBean("CoCo2", "50"); 77 UserMapperInterface userMapperInterface = session.getMapper(UserMapperInterface.class); 78 int n = userMapperInterface.insertUser(userBean); 79 // 提交 80 session.commit(); 81 System.out.println("插入數據成功:" + userBean); 82 } 83 84 @Ignore 85 @Test 86 public void testUpdateUser() { 87 UserBean userBean = new UserBean(2, "Tom44", "40"); 88 UserMapperInterface userMapperInterface = session.getMapper(UserMapperInterface.class); 89 userMapperInterface.updateUser(userBean); 90 session.commit(); 91 System.out.println("修改數據成功:" + userBean); 92 } 93 94 @Ignore 95 @Test 96 public void testDeleteUser() { 97 UserBean userBean = new UserBean(15, "XXX", "40"); 98 UserMapperInterface userMapperInterface = session.getMapper(UserMapperInterface.class); 99 int n = userMapperInterface.deleteByUserBean(userBean); 100 session.commit(); 101 System.out.println("刪除數據成功:" + userBean); 102 System.out.println("操作成功記錄數:" + n); 103 } 104 105 @Test 106 public void testSelectOneUserSalary() { 107 // 接口自動實例化 108 UserMapperInterface userMapperInterface = session.getMapper(UserMapperInterface.class); 109 // 執行sql 110 UserSalary userSalary = userMapperInterface.selectOneUserSalary("Tom"); 111 System.out.println(userSalary); 112 } 113 114 }
mybatis之註解方式實現