* 使用mybatis举例,使用XML方式实现
* 1、导入jar包:mybatis和mysql-connector
* 2、mybatis配置文件:mybatis-config.xml,指定加载Mapper文件路径
* 3、编写JavaBean类:UserBean
* 4、Mapper配置文件:UserMapper.xml
* 5、编写测试类进行测试
pom.xml文件关键内容如下:
<properties> <mybatis>3.4.5</mybatis> <mysql-connector>5.1.45</mysql-connector> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis}</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
mybatis-config.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 resource="UserBean.xml"/>--> 24 <mapper resource="first/UserMapper.xml"/> 25 26 </mappers> 27 </configuration>
jdbc.properties
1 # jdbc连接信息 2 driver=com.mysql.jdbc.Driver 3 url=jdbc:mysql://192.168.184.130:3306/gxrdb 4 username=root 5 password=root
UserMapper.xml
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="first.UserBean"> 7 <!--id:名称; parameterType传入参数类型; resultType:返回数据类型--> 8 <select id="selectOneUser" parameterType="int" resultType="first.UserBean"> 9 select * from user where id = #{id} 10 </select> 11 12 <!--批量查询--> 13 <select id="selectAllUser" resultMap="userList"> 14 select * from user 15 </select> 16 <resultMap type="first.UserBean" id="userList"> 17 <result property="id" column="id"/> 18 <result property="name" column="name"/> 19 <result property="age" column="age"/> 20 </resultMap> 21 22 <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> 23 insert into user (id, name, age) values (#{id} ,#{name}, #{age}) 24 </insert> 25 26 <update id="updateUser"> 27 update user set name=#{name} where id=#{id} 28 </update> 29 30 <delete id="deleteUser"> 31 delete from user where id=#{id} 32 </delete> 33 34 35 </mapper>
UserBean.class
1 package first; 2 3 /** 4 * 数据封装类 5 * JavaBean 6 */ 7 public class UserBean { 8 private int id; 9 private String name; 10 private String age; 11 12 public UserBean() { 13 14 } 15 16 public UserBean(String name, String age) { 17 this.name = name; 18 this.age = age; 19 } 20 21 public UserBean(int id, String name, String age) { 22 this.id = id; 23 this.name = name; 24 this.age = age; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public String getAge() { 36 return age; 37 } 38 39 public void setAge(String age) { 40 this.age = age; 41 } 42 43 public int getId() { 44 return id; 45 } 46 47 public void setId(int id) { 48 this.id = id; 49 } 50 51 @Override 52 public String toString() { 53 return "UserBean{id:" + id + "; 姓名:" + name + "; 年龄:" + age + "}"; 54 } 55 }
TestFirst.class
1 package first; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 import org.junit.Ignore; 8 import org.junit.Test; 9 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.util.List; 13 14 /* 15 * 使用mybatis举例,使用XML方式实现 16 * 1、导入jar包:mybatis和mysql-connector 17 * 2、mybatis配置文件:mybatis-config.xml,指定加载Mapper文件路径 18 * 3、编写JavaBean类:UserBean 19 * 4、Mapper配置文件:UserMapper.xml 20 * 5、编写测试类进行测试 21 * */ 22 public class TestFirst { 23 24 @Test 25 public void testSelectOneUser() { 26 SqlSession session = null; 27 try { 28 // mybatis配置文件 29 String resource = "mybatis-config.xml"; 30 InputStream inputStream = Resources.getResourceAsStream(resource); 31 // 创建工厂 32 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 33 // 创建session对象 34 session = sqlSessionFactory.openSession(); 35 // 执行sql,返回对象 36 UserBean userMapper = session.selectOne("first.UserBean.selectOneUser", 1); 37 System.out.println(userMapper); 38 UserBean userMapper2 = session.selectOne("first.UserBean.selectOneUser", 4); 39 System.out.println(userMapper2); 40 41 // 批量查询 42 List<UserBean> listUserBean = session.selectList("first.UserBean.selectAllUser"); 43 System.out.println("记录个数:" + listUserBean.size()); 44 System.out.println(listUserBean); 45 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } finally { 49 session.close(); 50 } 51 } 52 53 @Ignore 54 @Test 55 public void testInsertUser() { 56 SqlSession session = null; 57 try { 58 // mybatis配置文件 59 String resource = "mybatis-config.xml"; 60 InputStream inputStream = Resources.getResourceAsStream(resource); 61 // 创建工厂 62 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 63 // 创建session对象 64 session = sqlSessionFactory.openSession(); 65 // 执行sql,返回对象 66 UserBean userBean = new UserBean("CoCo", "40"); 67 int n = session.insert("first.UserBean.insertUser", userBean); 68 // 提交 69 session.commit(); 70 System.out.println("插入数据成功:" + userBean); 71 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } finally { 75 session.close(); 76 } 77 } 78 79 @Ignore 80 @Test 81 public void testUpdateUser() { 82 SqlSession session = null; 83 try { 84 // mybatis配置文件 85 String resource = "mybatis-config.xml"; 86 InputStream inputStream = Resources.getResourceAsStream(resource); 87 // 创建工厂 88 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 89 // 创建session对象 90 session = sqlSessionFactory.openSession(); 91 // 执行sql,返回对象 92 UserBean userBean = new UserBean(2, "Tom33", "40"); 93 int n = session.insert("first.UserBean.updateUser", userBean); 94 session.commit(); 95 System.out.println("修改数据成功:" + userBean); 96 97 } catch (IOException e) { 98 e.printStackTrace(); 99 } finally { 100 session.close(); 101 } 102 } 103 104 @Ignore 105 @Test 106 public void testDeleteUser() { 107 SqlSession session = null; 108 try { 109 // mybatis配置文件 110 String resource = "mybatis-config.xml"; 111 InputStream inputStream = Resources.getResourceAsStream(resource); 112 // 创建工厂 113 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 114 // 创建session对象 115 session = sqlSessionFactory.openSession(); 116 // 执行sql,返回对象 117 UserBean userBean = new UserBean(13, "XXX", "40"); 118 int n = session.insert("first.UserBean.deleteUser", userBean); 119 session.commit(); 120 System.out.println("删除数据成功:" + userBean); 121 System.out.println("操作成功记录数:" + n); 122 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } finally { 126 session.close(); 127 } 128 } 129 130 }