Mybatis总结:mybatis的搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis总结:mybatis的搭建相关的知识,希望对你有一定的参考价值。
- 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的搭建的主要内容,如果未能解决你的问题,请参考以下文章
springmvc学习总结 -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解