(一)问候MyBatis3
第一节:MyBatis简介
百度百科
第二季:Mybatis版HolleWorld实现
例子:
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 <properties resource="jdbc.properties"/> <!-- 引入jdbc.properties文件 --> 7 <!-- 类型别名 --> 8 <typeAliases> 9 <typeAlias alias="Student" type="com.wishwzp.model.Student"/> 10 </typeAliases> 11 <!-- 开发环境,这里就写了一个id为development的数据环境 --> 12 <environments default="development"> 13 <environment id="development"> 14 <transactionManager type="JDBC" /> 15 <dataSource type="POOLED"> 16 <property name="driver" value="${jdbc.driverClassName}" /> 17 <property name="url" value="${jdbc.url}" /> 18 <property name="username" value="${jdbc.username}" /> 19 <property name="password" value="${jdbc.password}" /> 20 </dataSource> 21 </environment> 22 </environments> 23 24 <mappers> 25 <mapper resource="com/wishwzp/mappers/StudentMapper.xml" /> 26 </mappers> 27 28 </configuration>
jdbc.properties:
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=utf-8 3 jdbc.username=root 4 jdbc.password=root
StudentTest.java:
1 package com.wishwzp.service; 2 3 import org.apache.ibatis.session.SqlSession; 4 5 import com.wishwzp.mappers.StudentMapper; 6 import com.wishwzp.model.Student; 7 import com.wishwzp.util.SqlSessionFactoryUtil; 8 9 public class StudentTest { 10 11 public static void main(String[] args) { 12 SqlSession sqlSession=SqlSessionFactoryUtil.openSession(); 13 StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); 14 //do work 15 Student student=new Student("李四",11); 16 int result=studentMapper.add(student); 17 sqlSession.commit(); 18 if(result>0){ 19 System.out.println("添加成功!"); 20 } 21 } 22 }
SqlSessionFactoryUtil.java:
1 package com.wishwzp.util; 2 3 import java.io.InputStream; 4 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 10 public class SqlSessionFactoryUtil { 11 12 private static SqlSessionFactory sqlSessionFactory; 13 14 public static SqlSessionFactory getSqlSessionFactory(){ 15 if(sqlSessionFactory==null){ 16 InputStream inputStream=null; 17 try{ 18 inputStream=Resources.getResourceAsStream("mybatis-config.xml"); 19 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); 20 }catch(Exception e){ 21 e.printStackTrace(); 22 } 23 } 24 return sqlSessionFactory; 25 } 26 27 public static SqlSession openSession(){ 28 return getSqlSessionFactory().openSession(); 29 } 30 }
Student.java:
1 package com.wishwzp.model; 2 3 public class Student { 4 5 private Integer id; 6 private String name; 7 private Integer age; 8 9 public Student() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 14 public Student(String name, Integer age) { 15 super(); 16 this.name = name; 17 this.age = age; 18 } 19 20 public Integer getId() { 21 return id; 22 } 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 public String getName() { 27 return name; 28 } 29 public void setName(String name) { 30 this.name = name; 31 } 32 public Integer getAge() { 33 return age; 34 } 35 public void setAge(Integer age) { 36 this.age = age; 37 } 38 39 }
StudentMapper.java:
1 package com.wishwzp.mappers; 2 3 import com.wishwzp.model.Student; 4 5 public interface StudentMapper { 6 7 public int add(Student student); 8 }
StudentMapper.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="com.wishwzp.mappers.StudentMapper"> 6 7 <insert id="add" parameterType="Student" > 8 insert into t_student values(null,#{name},#{age}) 9 </insert> 10 11 </mapper>
数据库为db_mybatis: