MyBatis-----初识MyBatis--封装---别名---properties
Posted Angelye
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis-----初识MyBatis--封装---别名---properties相关的知识,希望对你有一定的参考价值。
一、核心配置文件
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 <!-- 最开始加载引用db.properties文件 --> 6 <properties resource="db.properties"/> 7 <!-- 取别名 以下情况别名为类名 --> 8 <typeAliases> 9 <package name="com.myBatis.entity"/> 10 </typeAliases> 11 <!-- 12 development : 开发模式 13 work : 工作模式 14 --> 15 <environments default="development"> 16 <environment id="development"> 17 <transactionManager type="JDBC" /> 18 <!-- 配置数据库连接信息 --> 19 <dataSource type="POOLED"> 20 <property name="driver" value="${driver}" /> 21 <property name="url" value="${url}" /> 22 <property name="username" value="${username}" /> 23 <property name="password" value="${password}" /> 24 </dataSource> 25 </environment> 26 </environments> 27 <!-- 引用映射文件 --> 28 <mappers > 29 <mapper resource="com/myBatis/mapper/userMapper.xml"/> 30 </mappers> 31 </configuration>
二、实体类
1 package com.myBatis.entity; 2 3 public class EUser { 4 private Integer id; 5 private String name; 6 private Integer age; 7 public int getId() { 8 return id; 9 } 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public Integer getAge() { 17 return age; 18 } 19 public void setAge(Integer age) { 20 this.age = age; 21 } 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 @Override 26 public String toString() { 27 return "EUser [id=" + id + ", name=" + name + ", age=" + age + "]"; 28 } 29 30 31 32 }
三、实体类的映射文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 5 为这个mapper指定一个唯一的namespace,namespace的值习惯上设置成包名+sql映射文件名, 6 这样就能够保证namespace的值是唯一的 7 --> 8 <mapper namespace="com.myBatis.mapper.userMapper.xml"> 9 <!-- 根据id查询得到一个user对象 --> 10 <select id="getUser" parameterType="int" resultType="EUser"> 11 select * from users where id=#{id} 12 </select> 13 <!-- 根据id查询删除一个user对象 --> 14 <delete id="deleteUser" parameterType="EUser"> 15 delete from users where id=#{id} 16 </delete> 17 <!-- 创建用户(Create) --> 18 <insert id="addUser" parameterType="EUser"> 19 insert into users values(null,#{name},#{age}) 20 </insert> 21 <!-- 修改用户(Update) --> 22 <update id="updateUser" parameterType="EUser"> 23 update users set name=#{name},age=#{age} where id=#{id} 24 </update> 25 </mapper> 26
四、封装类
1 package com.myBatis.util; 2 3 4 import java.io.IOException; 5 import java.io.Reader; 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 12 public class myBtaisUtil { 13 static Reader reader=null; 14 static SqlSessionFactory sqlSessionFactory= null; 15 static SqlSession sqlSession = null; 16 /** 17 * 加载核心配置文件 18 * 获取SqlSessionFactory 19 * @return SqlSessionFactory 20 */ 21 public static SqlSessionFactory getsqlSessionFactory() { 22 try { 23 reader = Resources.getResourceAsReader("SqlMapConfig.xml"); 24 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 return sqlSessionFactory; 29 } 30 /** 31 * 获取SqlSession 32 * @return SqlSession 33 */ 34 public static SqlSession getsqlSession() { 35 return sqlSession= getsqlSessionFactory().openSession(true); 36 } 37 }
五、properties文件
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql:///mybatis 3 username=root 4 password=root
六、测试类
1 package myBatisTest; 2 import java.io.IOException; 3 import java.io.Reader; 4 import org.apache.ibatis.io.Resources; 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 import org.junit.Test; 9 10 import com.myBatis.entity.EUser; 11 import com.myBatis.util.myBtaisUtil; 12 13 public class TestUser { 14 //修改 15 @Test 16 public void testUpdateUser() { 17 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 18 //先查找 19 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 20 EUser user = sqlSession.selectOne(select, 7); 21 System.out.println(user); 22 //再修改 23 user.setName("改啦7777"); 24 user.setAge(50); 25 String update = "com.myBatis.mapper.userMapper.xml.updateUser"; 26 int i = sqlSession.update(update, user); 27 System.out.println(i); 28 EUser user1= sqlSession.selectOne(select, 7); 29 System.out.println(user1); 30 } 31 //添加 32 @Test 33 public void testAddUser() { 34 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 35 String add = "com.myBatis.mapper.userMapper.xml.addUser"; 36 EUser user = new EUser(); 37 user.setName("用户孤傲苍狼"); 38 user.setAge(20); 39 int i = sqlSession.insert(add, user); 40 System.out.println(i); 41 } 42 //删除 43 @Test 44 public void testDeleteUser() { 45 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 46 String delete = "com.myBatis.mapper.userMapper.xml.deleteUser"; 47 int i= sqlSession.delete(delete, 6); 48 System.out.println(i); 49 } 50 //查询 51 @Test 52 public void testGetUser() { 53 SqlSession sqlSession=myBtaisUtil.getsqlSession(); 54 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 55 EUser user = sqlSession.selectOne(select, 1); 56 System.out.println(user); 57 } 58 }
注:
类的映射文件中的<mapper namespace="com.myBatis.mapper.userMapper.xml"> 映射文件后缀名可不写
测试类中的 String select = "com.myBatis.mapper.userMapper.xml.getUser"; 映射文件后缀名可不写
参考:MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
MyBatis学习总结(三)——优化MyBatis配置文件中的配置
以上是关于MyBatis-----初识MyBatis--封装---别名---properties的主要内容,如果未能解决你的问题,请参考以下文章