mybatis之注解方式实现
* 使用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 }