MyBatis入门(一)SqlSessionFactory
(1)新建数据库
1 CREATE TABLE emp ( 2 id INT ( 10 ) PRIMARY KEY auto_increment, 3 last_name VARCHAR(255), 4 gender CHAR(1), 5 email VARCHAR(255) 6 )
(2)输入数据
INSERT INTO emp VALUES(1,"张三","男","we.com");
(3)新建项目
(4)导入Jar包
(5)编写log4j
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 3 4 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 5 6 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> 7 <param name="Encoding" value="UTF-8" /> 8 <layout class="org.apache.log4j.PatternLayout"> 9 <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> 10 </layout> 11 </appender> 12 <logger name="java.sql"> 13 <level value="debug" /> 14 </logger> 15 <logger name="org.apache.ibatis"> 16 <level value="info" /> 17 </logger> 18 <root> 19 <level value="debug" /> 20 <appender-ref ref="STDOUT" /> 21 </root> 22 </log4j:configuration>
(6)编写实体类
1 public class Emp { 2 3 private Integer id; 4 private String lastName; 5 private String geder; 6 private String email; 7 public Emp() { 8 super(); 9 } 10 public Emp(Integer id, String lastName, String geder, String email) { 11 super(); 12 this.id = id; 13 this.lastName = lastName; 14 this.geder = geder; 15 this.email = email; 16 } 17 public Integer getId() { 18 return id; 19 } 20 public void setId(Integer id) { 21 this.id = id; 22 } 23 public String getLastName() { 24 return lastName; 25 } 26 public void setLastName(String lastName) { 27 this.lastName = lastName; 28 } 29 public String getGeder() { 30 return geder; 31 } 32 public void setGeder(String geder) { 33 this.geder = geder; 34 } 35 public String getEmail() { 36 return email; 37 } 38 public void setEmail(String email) { 39 this.email = email; 40 } 41 @Override 42 public String toString() { 43 return "Emp [id=" + id + ", lastName=" + lastName + ", geder=" + geder + ", email=" + email + "]"; 44 }
(7)编写MyBatis的配置文件
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 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="com.mysql.jdbc.Driver"/> 11 <property name="url" value="jdbc:mysql://localhost:3306/MyBatis"/> 12 <property name="username" value="root"/> 13 <property name="password" value="root"/> 14 </dataSource> 15 </environment> 16 </environments> 17 <mappers> 18 <mapper resource="conf/EmpMapper.xml"/> 19 </mappers> 20 </configuration>
(8)编写Mapper文件EmpMapper.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 <mapper namespace="conf.EmpMapper"> 6 <select id="selectEmp" resultType="com.eu.pojo.Emp"> 7 select id,last_name lastName,gender,email from Emp where id = #{id} 8 </select> 9 </mapper>
(9)编写测试类
1 @Test 2 public void testMapper() throws IOException { 3 String resource = "conf/mybatis-config.xml"; 4 InputStream inputStream = Resources.getResourceAsStream(resource); 5 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 6 7 SqlSession session = sqlSessionFactory.openSession(); 8 try { 9 Emp emp = (Emp) session.selectOne("conf.EmpMapper.selectEmp", 1); 10 System.out.println(emp); 11 } finally { 12 session.close(); 13 } 14 }
(10)测试结果
1 DEBUG 04-14 19:20:32,007 ==> Preparing: select id,last_name lastName,gender,email from Emp where id = ? (BaseJdbcLogger.java:145) 2 DEBUG 04-14 19:20:32,073 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145) 3 DEBUG 04-14 19:20:32,107 <== Total: 1 (BaseJdbcLogger.java:145) 4 Emp [id=1, lastName=张三, geder=null, email=we.com]
这种方式还存在缺陷,
Emp emp = (Emp) session.selectOne("conf.EmpMapper.selectEmp", 1);
如果传入一个字符呢?如何查?所以必须限制传入的是合法的数字。针对这种方式进行了修改,见下一篇 MyBatis入门(二)接口式编程