Mybatis總結(一):mybatis的搭建
阿新 • • 發佈:2017-11-26
ack pen devel column 3.3 exception ive pri tac
- mybatis:它抽象了大量的jdbc代碼,並提供了一個簡單易用的API和數據庫交互。
- mybatis的優勢:它消除了大量jdbc冗余的代碼、它可以接受SQL語句。
- mybatis的jar包:核心包只有一個mybatis-3.x.0.jar
- mybatis框架需要的兩種文件:
- 配置文件:
功能:主要放與數據庫連接相關的信息
命名規則:mybatis-config.xml
位置:src下
- 映射文件:
功能:主要放SQL語句
命名規則:XxxMapper.xml(Xxx一般是JavaBean的類名)
位置:src下
- Mybatis的映射接口
它與映射文件XxxMapper.xml中的SQL語句進行映射。
功能:它是調用SQL語句執行的接口。
命名規則:接口中的方法名字要與xml文件定義的SQL映射語句的名稱相同。
同時我們不需要去實現該接口,因為mybatis中提供了相應的方式在運行期間動態生成該接口的實現類對象(動態代理技術)。
- Mybatis中的SqlSession接口和SqlSessionFactory接口
SqlSession接口:它實現的對象是mybatis中最重要的一個對象,我們可以使用該對象動態的獲得XxxMapper.java接口的實現對象,然後就可以調用到XxxMapper.java接口中方法映射的sql語句。
SqlSessionFactroy接口:它是專門負責生產sqlSession對象的。
Mybatis搭建實例:
第一步:導包:
mybatis-3.3.0.jar ojdbc14.jar
junit-4.7.jar log4j-1.2.17.jar
第二步:在src下創建配置文件mybatis-config.xml和映射文件XxxMapper.xml。
Mybaties-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.zzuli.bean.Student" alias="Student" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" /> <property name="username" value="sahe" /> <property name="password" value="sahe" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/zzuli/Mapper/StudentMapper.xml"/> </mappers> </configuration>
XxxMapper.xml映射文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zzuli.Mapper.StudentMapper"> <resultMap type="Student" id="StudentResult"> <id property="stuId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <result property="dob" column="dob" /> </resultMap> <select id="findAllStudents" resultMap="StudentResult"> SELECT * FROM STUDENTS </select> <select id="findStudentById" parameterType="int" resultType="Student"> SELECT STUD_ID AS STUDID,NAME,EMAIL,DOB FROM STUDENTS WHERE STUD_ID=#{id} </select> <insert id="insertStudent" parameterType="Student"> INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB) VALUES(#{stuId},#{name},#{email},#{dob}) </insert> </mapper>
第三步:創建Student對象
package com.zzuli.bean; import java.util.Date; public class Student { private int stuId; private String name; private String email; private Date dob; public Student(int stuId, String name, String email, Date dob) { super(); this.stuId = stuId; this.name = name; this.email = email; this.dob = dob; } public Student(){} public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getDob() { return dob; } public void setDob(Date dob) { this.dob = dob; } @Override public String toString() { return "Student [stuId=" + stuId + ", name=" + name + ", email=" + email + ", dob=" + dob + "]"; } } 第四步:創建XxxMappeer.java接口 package com.zzuli.Mapper import java.util.List; import com.zzuli.bean.Student; public interface StudentMapper { List<Student> findAllStudents(); Student findStudentById(Integer id); void insertStudent(Student student); }
第五步:創建test測試
import com.zzuli.Mapper.StudentMapper; import com.zzuli.bean.Student; public class StudentMapperTest { @Test public void testStudnet(){ try { InputStream inputs = Resources.getResourceAsStream("mybaties-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputs); SqlSession session = sqlSessionFactory.openSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); Student student = new Student(1,"tom","[email protected]",new Date()); mapper.insertStudent(student); session.commit(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
對mybatis的一些基本封裝
public class MyBatisSqlSessionFactory { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory(){ InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybaties-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e.getCause()); } return sqlSessionFactory; } public static SqlSession openSession() { return openSession(false); } public static SqlSession openSession(boolean autoCommit) { return getSqlSessionFactory().openSession(autoCommit); } }
Mybatis總結(一):mybatis的搭建