MyBatis入门:
第一天接触Mybatis,总结一下入门案例的流程:
首先导入Mybatis的jar包和数据库的驱动包
1.创建数据表和实体类
2.创建一个表和实体类映射的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="com.offcn.entity.personMapper"> 7 8 9 <select id="selectPersonById" parameterType="int" resultType="com.offcn.entity.Person"> 10 select * from Person where id = #{id} 11 select> 12 13 14 <insert id="insertPerson" parameterType="com.offcn.entity.Person"> 15 insert into Person (id,name,bir,address) value (#{id},#{name},#{bir},#{address}) 16 insert> 17 18 <delete id="deletePersonById" parameterType="int"> 19 delete from Person where id = #{id} 20 delete> 21 22 <update id="updatePerson" parameterType="com.offcn.entity.Person"> 23 update person set name=#{name},bir=#{bir},address=#{address} where id = #{id} 24 update> 25 26 <select id="selectPerson" resultType="com.offcn.entity.Person"> 27 select * from person 28 select> 29 30 mapper>
3.编写全局配置文件conf.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 7 <environments default="development"> 8 <environment id="development"> 9 14 <transactionManager type="JDBC"/> 15 16 21 <dataSource type="POOLED"> 22 <property name="driver" value="com.mysql.jdbc.Driver"/> 23 <property name="url" value="jdbc:mysql://localhost:3306/person?serviceTimeout=UTC"/> 24 <property name="username" value="root"/> 25 <property name="password" value="root"/> 26 dataSource> 27 environment> 28 environments> 29 <mappers> 30 <mapper resource="com/offcn/entity/personMapper.xml"/> 31 mappers> 32 configuration>
3.写一个测试类,进行增删改查的测试,步骤如下:
1 package com.offcn.entity; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 import java.util.Date; 6 import java.util.List; 7 8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession; 10 import org.apache.ibatis.session.SqlSessionFactory; 11 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 12 13 public class Test { 14 public static void main(String[] args) throws IOException { 15 // selectPerosnById(); 16 //insertPerson(); 17 //deletePersonById(); 18 //updatePerson(); 19 selectPerson(); 20 } 21 //根据id查询 22 public static void selectPerosnById() throws IOException { 23 //读取conf.xml 配置文件 24 Reader reader = Resources.getResourceAsReader("conf.xml"); 25 //创建sqlSessionFactory 26 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 27 //创建session---> connection 28 SqlSession session = sessionFactory.openSession(); 29 //statement ---> statement 30 String sql = "com.offcn.entity.personMapper." + "selectPersonById"; 31 //查询一条数据 32 Person person = session.selectOne(sql,1); 33 //提交事务 34 session.commit(); 35 System.out.println(person); 36 //关闭连接 37 session.close(); 38 39 } 40 //增加一条数据 41 public static void insertPerson() throws IOException { 42 //读取conf.xml文件 43 Reader reader = Resources.getResourceAsReader("conf.xml"); 44 //创建一个sqlSessionFactory 45 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 46 //创建一个session对象 47 SqlSession session = sessionFactory.openSession(); 48 //定位sql语句 49 String statement = "com.offcn.entity.personMapper." + "insertPerson"; 50 //执行sql语句 51 Person person = new Person("李四",new Date(),"上海普陀"); 52 int count = session.insert(statement,person); 53 //提交事务 54 session.commit(); 55 System.out.println("增加"+count+"条数据"); 56 //关闭连接 57 session.close(); 58 } 59 //根据id删除一条数据 60 public static void deletePersonById() throws IOException { 61 //读取conf.xml文件 62 Reader reader = Resources.getResourceAsReader("conf.xml"); 63 //获取sqlSessionFactory 64 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 65 //获取session对象 66 SqlSession session = sessionFactory.openSession(); 67 //定位sql语句 68 String statement = "com.offcn.entity.personMapper."+"deletePersonById"; 69 //执行sql语句 70 int count = session.delete(statement,2); 71 //提交事务 72 session.commit(); 73 System.out.println("删除"+count+"条数据"); 74 //关闭连接 75 session.close(); 76 } 77 //更改数据 78 public static void updatePerson() throws IOException { 79 //读取conf.xml 80 Reader reader = Resources.getResourceAsReader("conf.xml"); 81 //创建sqlSessionFactory 82 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 83 //创建sqlsession对象 84 SqlSession session = sessionFactory.openSession(); 85 //定位sql 86 String statement = "com.offcn.entity.personMapper." + "updatePerson"; 87 //执行sql语句 88 Person person = new Person(); 89 person.setName("王五"); 90 person.setBir(new Date()); 91 person.setAddress("南京"); 92 person.setId(1); 93 int count = session.update(statement,person); 94 //提交事务 95 session.commit(); 96 System.out.println("更改" + count +"条数据成功"); 97 //关闭连接 98 session.close(); 99 } 100 //查询所有数据 101 public static void selectPerson() throws IOException { 102 //读取conf.xml 103 Reader reader = Resources.getResourceAsReader("conf.xml"); 104 //创建sqlSessionFactory 105 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 106 //创建sqlsession对象 107 SqlSession session = sessionFactory.openSession(); 108 //定位sql 109 String statement = "com.offcn.entity.personMapper." + "selectPerson"; 110 //执行sql语句 111 Listpersons = session.selectList(statement); 112 //提交事务 113 session.commit(); 114 for (Person person : persons) { 115 System.out.println(person); 116 } 117 118 //关闭连接 119 session.close(); 120 } 121 }
到此就完成了一个Mybatis的入门程序。