①创建数据库和表,数据库为mytest,表为users
1 CREATE DATABASE mytest; 2 USE mytest; 3 4 5 DROP TABLE IF EXISTS users; 6 CREATE TABLE users( 7 id INT PRIMARY KEY auto_increment NOT NULL, 8 name VARCHAR(20), 9 age INT 10 )
②创建Java工程,导入相应的jar包
③创建配置文件conf.xml和数据库配置文件db.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 5 <configuration> 6 7 <properties resource="db.properties"></properties> 8 <environments default="development"> 9 <environment id="development"> 10 <transactionManager type="JDBC"></transactionManager> 11 <dataSource type="POOLED"> 12 <property name="driver" value="${driver}"/> 13 <property name="url" value="${url}"/> 14 <property name="username" value="${name}"/> 15 <property name="password" value="${password}"/> 16 </dataSource> 17 </environment> 18 </environments> 19 </configuration>
1 driver = com.mysql.jdbc.Driver 2 url = jdbc:mysql://localhost:3306/mytest 3 name = root 4 password = root
④创建实体类User
1 package org.mybatis.domain; 2 3 public class User { 4 5 private int id; 6 private String name; 7 private int age; 8 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 @Override 28 public String toString() { 29 return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; 30 } 31 32 }
⑤创建sql映射文件userMapper.xml
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="org.mybatis.mapping.userMapper"> 6 <!-- 根据id查询得到一个user对象 --> 7 <select id="getUser" parameterType="int" resultType="org.mybatis.domain.User"> 8 select * from users where id=#{id} 9 </select> 10 11 <select id="getAllUsers" resultType="org.mybatis.domain.User"> 12 select * from users 13 </select> 14 15 <insert id="addUser" parameterType="org.mybatis.domain.User"> 16 insert into users(name, age) values (#{name}, #{age}) 17 </insert> 18 19 <update id="updateUser" parameterType="org.mybatis.domain.User"> 20 update users set name=#{name}, age=#{age} where id=#{id} 21 </update> 22 23 <delete id="deleteUser" parameterType="int"> 24 delete from users where id=#{id} 25 </delete> 26 </mapper>
⑥向配置文件中注册 userMapper.xml 文件
1 <mappers> 2 <mapper resource="org/mybatis/mapping/userMapper.xml"/> 3 </mappers> 4 5 6 </configuration>
⑦测试类
1 package org.mybatis.app; 2 3 import java.io.InputStream; 4 import java.util.List; 5 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.Before; 10 import org.junit.Test; 11 import org.mybatis.domain.User; 12 13 public class TestXML { 14 15 SqlSession session; 16 17 @Before 18 public void beforeLoadXML(){ 19 //加载 mybatis 配置文件 20 InputStream inputStream = TestXML.class. 21 getClassLoader().getResourceAsStream("conf.xml"); 22 //构建sqlSession的工厂 23 SqlSessionFactory sqlSessionFactory = 24 new SqlSessionFactoryBuilder().build(inputStream); 25 //根据 sqlSessionFactory 产生 session 26 session = sqlSessionFactory.openSession(); 27 } 28 29 //插入数据 30 @Test 31 public void addOneUser() { 32 String statement = "org.mybatis.mapping.userMapper.addUser"; 33 User user = new User(); 34 user.setName("666"); 35 user.setAge(66); 36 session.insert(statement, user); 37 session.commit(); 38 session.close(); 39 } 40 41 42 //根据id查询user表中的数据 43 @Test 44 public void getOneUser(){ 45 String statement = "org.mybatis.mapping.userMapper.getUser"; 46 User user = session.selectOne(statement, 1); 47 System.out.println(user); 48 session.close(); 49 } 50 51 //查询所有数据 52 @Test 53 public void getAllUser() { 54 String statement = "org.mybatis.mapping.userMapper.getAllUsers"; 55 List<User> listUser = session.selectList(statement); 56 System.out.println(listUser); 57 session.close(); 58 } 59 60 //更新数据 61 //session.update(statement, user); 62 //删除数据 63 //session.delete(statement, 1); 64 }
⑧结构图为: