【练习】mybatis
1、
1 jdbc.driver_class=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://127.0.0.1:3306/smbms?zeroDateTimeBehavior=convertToNull 3 jdbc.username=root 4 jdbc.password=123456
2、
<?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> <!--加载--> <properties resource="database.properties"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/smbms?zeroDateTimeBehavior=convertToNull" /> <property name="username" value="root" /> <property name="password" value="123456" /> </properties> <!--设置日志行为--> <!-- <settings>--> <!-- <setting name="logImpl" value="LOG4J"/>--> <!-- </settings>--> <typeAliases> <!-- //不推荐使用,当我们有很多的实体类的时候,这里配置的也很冗余--> <!-- <typeAlias type="cn.kgc.pojo.User"></typeAlias>--> <package name="cn.kgc.pojo"></package> </typeAliases> <!--更改运行环境--> <environments default="test"> <!-- 配置运行环境--> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="test"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/kgc/dao/UserMapper.xml"/> </mappers> </configuration>
3、pojo实体类
4、dao
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="cn.kgc.dao.UserMapper"> 7 8 <!-- 查询用户表记录数 --> 9 <select id="count" resultType="int"> 10 select count(1) as count from smbms_user 11 </select> 12 13 <!-- 查询用户列表 1 --> 14 <select id="userlist" resultType="User"> 15 select * from smbms_user 16 </select> 17 <!-- 查询用户列表 2 --> 18 <select id="getUserListByNullParam" resultType="cn.kgc.pojo.User"> 19 select * from smbms_user 20 </select> 21 22 <!-- 查询供应商记录数--> 23 <select id="result" resultType="int"> 24 select count(1) as count from smbms_provider 25 </select> 26 <!-- 查询供应商列表 1接口 --> 27 <select id="getProviderList" resultType="cn.kgc.pojo.Provider"> 28 select * from smbms_provider 29 </select> 30 <!-- 查询供应商列表 2工具 --> 31 <select id="providerlist" resultType="cn.kgc.pojo.Provider"> 32 select * from smbms_provider 33 </select> 34 35 </mapper>
.
1 public interface UserMapper { 2 public List<User> getUserListByNullParam(); 3 public List<Provider> getProviderList(); 4 }
5.util
1 public class MybatisUtils { 2 private static SqlSessionFactory sqlSessionFactory; 3 static { 4 String path="mybatis-config.xml"; 5 try { 6 InputStream is= Resources.getResourceAsStream(path); 7 sqlSessionFactory=new SqlSessionFactoryBuilder().build(is); 8 } catch (IOException e) { 9 e.printStackTrace(); 10 } 11 12 } 13 14 public static SqlSession getSqlSession() { 15 return sqlSessionFactory.openSession(); 16 } 17 }
6.test
1 package cn.kgc.test; 2 3 import cn.kgc.dao.UserMapper; 4 import cn.kgc.pojo.Provider; 5 import cn.kgc.pojo.User; 6 import cn.kgc.util.MybatisUtils; 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 import org.apache.log4j.Logger; 13 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.util.List; 17 18 /** 19 * Created with IntelliJ IDEA. 20 * User: L 21 * Date: 2019/10/21 22 * Time: 9:52 23 * Description: No Description 24 */ 25 public class TestUserMapper { 26 27 /** 28 * 查询用户条数 29 */ 30 @Test 31 public void test01(){ 32 //加载核心配置文件 33 String path="mybatis-config.xml"; 34 InputStream is=null; 35 try { 36 is= Resources.getResourceAsStream(path); 37 //mybatis中的核心类 38 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is); 39 SqlSession sqlSession=sqlSessionFactory.openSession(); 40 int count = sqlSession.selectOne("cn.kgc.dao.UserMapper.count"); 41 System.out.println("一共"+count+"条用户记录。"); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 46 } 47 48 /** 49 * 测试输出用户列表 50 * 工具类 51 */ 52 @Test 53 public void test02(){ 54 SqlSession sqlSession= MybatisUtils.getSqlSession(); 55 List<User> userList=sqlSession.selectList("cn.kgc.dao.UserMapper.userlist"); 56 for (User user:userList){ 57 System.out.println("**用户名**"+user.getUserName()+"--"+user.getPhone()+"--"+user.getAddress()); 58 } 59 } 60 61 /** 62 * 输出用户列表 63 * 接口方式 64 */ 65 @Test 66 public void test03(){ 67 SqlSession sqlSession = MybatisUtils.getSqlSession(); 68 List<User> userList = sqlSession.getMapper(UserMapper.class).getUserListByNullParam(); 69 for (User user : userList) { 70 System.out.println("**用户名**"+user.getUserName()+"--"+user.getPhone()+"--"+user.getAddress()); 71 } 72 73 } 74 75 /** 76 * 查询供应商数 77 */ 78 @Test 79 public void test04(){ 80 String path="mybatis-config.xml"; 81 InputStream is=null; 82 try { 83 is= Resources.getResourceAsStream(path); 84 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is); 85 SqlSession sqlSession=sqlSessionFactory.openSession(); 86 int result = sqlSession.selectOne("cn.kgc.dao.UserMapper.result"); 87 System.out.println("一共"+result+"条供应商记录。"); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 /** 94 * 输出供应商详情 95 * 接口 96 */ 97 @Test 98 public void test05(){ 99 SqlSession sqlSession = MybatisUtils.getSqlSession(); 100 List<Provider> providerList = sqlSession.getMapper(UserMapper.class).getProviderList(); 101 for (Provider provider : providerList) { 102 System.out.println("**供应商**"+provider.getProName()+"--"+provider.getProContact()+"--"+provider.getProPhone()+"--"+provider.getProDesc()); 103 } 104 } 105 106 /** 107 * 输出供应商详情 108 * 工具 109 */ 110 @Test 111 public void test06(){ 112 SqlSession sqlSession= MybatisUtils.getSqlSession(); 113 List<Provider> providerList=sqlSession.selectList("cn.kgc.dao.UserMapper.providerlist"); 114 for (Provider provider:providerList){ 115 System.out.println("**供应商**"+provider.getProName()+"--"+provider.getProContact()+"--"+provider.getProPhone()+"--"+provider.getProDesc()); 116 } 117 } 118 119 120 }