Mybatis-----CRUD
学而不思则罔,思而不学则殆。
mybatis 增删改查,用到了MyEclipse中自带的Junit4做单元测试,没啥难度,作为练习。关于jar包和目录结构请参考上一篇,这里直接上代码。
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 <environments default="development"> 6 <environment id="development"> 7 <transactionManager type="JDBC" /> 8 <!-- 配置数据源 相当于JDBC连接数据库 9 Class.forName("oracle.jdbc.driver.OracleDriver"); 10 String user = "scott"; 11 String password = "tiger"; 12 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; 13 --> 14 <dataSource type="POOLED"> 15 <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> 16 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> 17 <property name="username" value="scott" /> 18 <property name="password" value="tiger" /> 19 </dataSource> 20 </environment> 21 </environments> 22 <!-- 读取Mapper包下的UserinfoMapper.xml文件 --> 23 <mappers> 24 <mapper resource="Mapper/UserinfoMapper.xml" /> 25 </mappers> 26 </configuration>
1 package TEST; 2 3 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 import org.junit.Test; 12 13 import bean.Userinfo; 14 15 16 public class Demo { 17 18 19 //根据ID查询用户 20 @Test 21 public void findbyuserinfoID() throws Throwable{ 22 23 // TODO Auto-generated method stub 24 25 // 加载Mybatis的主配置文件 26 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); 27 28 // 创建生产session的工厂类 session不是作用域 -- 相当于connection 29 SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder(); 30 SqlSessionFactory ssf = sfb.build(is); 31 // 打开session 32 SqlSession session=ssf.openSession(); 33 //执行sql语句 34 35 String name=session.selectOne("UserinfoMapper.getuser", 3); 36 System.out.println("name:"+ name); 37 session.close(); 38 System.out.println("over"); 39 40 } 41 42 //添加用户 43 @Test 44 public void insertuserinfo() throws Throwable{ 45 46 System.out.println("添加用户"); 47 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); 48 49 // 创建生产session的工厂类 session不是作用域 -- 相当于connection 50 SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder(); 51 SqlSessionFactory ssf = sfb.build(is); 52 // 打开session 53 SqlSession session=ssf.openSession(); 54 //执行sql语句 55 int result=session.delete("UserinfoMapper.deleteuser", 3); 56 //String name=session.selectOne("UserinfoMapper.deleteUser", 1); 57 System.out.println("result:"+ result); 58 //如果删除没有成功,可能是因为没有提交事务 一定要注意 59 session.commit(); 60 session.close(); 61 } 62 63 //删除用户 64 @Test 65 66 public void deleteuserinfoid() throws Throwable{ 67 System.out.println("删除用户"); 68 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); 69 70 // 创建生产session的工厂类 session不是作用域 -- 相当于connection 71 SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder(); 72 SqlSessionFactory ssf = sfb.build(is); 73 // 打开session 74 SqlSession session=ssf.openSession(); 75 //执行sql语句 76 // int result=session.delete("UserinfoMapper.deleteuser", 3); 77 Userinfo u = new Userinfo(); 78 u.setUname("文刀山豆几"); 79 u.setUpass("123"); 80 u.setId(5); 81 82 int Statement = session.insert("UserinfoMapper.adduser", u); 83 //String name=session.selectOne("UserinfoMapper.deleteUser", 1); 84 System.out.println("result:"+ Statement); 85 //如果删除没有成功,可能是因为没有提交事务 一定要注意 86 session.commit(); 87 session.close(); 88 } 89 //修改用户 90 @Test 91 public void updateuserinfo() throws Throwable{ 92 93 System.out.println("修改用户"); 94 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); 95 96 // 创建生产session的工厂类 session不是作用域 -- 相当于connection 97 SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder(); 98 SqlSessionFactory ssf = sfb.build(is); 99 // 打开session 100 SqlSession session=ssf.openSession(); 101 //执行sql语句 102 // int result=session.delete("UserinfoMapper.deleteuser", 3); 103 104 Userinfo user = new Userinfo(); 105 user.setId(1); 106 user.setUname("aaaaa"); 107 user.setUpass("fff"); 108 System.out.println("aaa"); 109 int a = session.update("UserinfoMapper.updateuser",user); 110 111 //String name=session.selectOne("UserinfoMapper.deleteUser", 1); 112 System.out.println("a:"+ a); 113 //如果删除没有成功,可能是因为没有提交事务 一定要注意 114 session.commit(); 115 session.close(); 116 } 117 118 }
下一篇会写关于配置文件优化,联合查询,以及使用注解的方式,客官们不要着急!