读书人

MyBatis3 入门学习-简略CRUD应用

发布时间: 2012-08-25 10:06:20 作者: rapoo

MyBatis3 入门学习--简单CRUD应用
一、简介:什么是MyBatis?
MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除
了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML
或注解用于配置和原始映射,将接口和 Java 的POJOs(Plan Old Java Objects,普通的 Java
对象)映射成数据库中的记录。

二、环境介绍
MyBatis3.0.3 + MySQL5.5.8
数据库表:student
<?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><properties resource="init.properties"> </properties> <typeAliases><typeAlias type="com.carvin.mybatis.study01.model.Student" alias="Student"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /></dataSource></environment></environments><mappers><mapper resource="com/carvin/mybatis/study01/config/student.xml" /></mappers></configuration>

init.properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8username=rootpassword=password


2、创建SqlSessionFactory的工具类
public class SqlSessionFactoryUtil {private static SqlSessionFactory sqlSessionFactory = null;static {String resource = "Configuration.xml";try {Reader reader = Resources.getResourceAsReader(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (IOException e) {e.printStackTrace();}}public static SqlSessionFactory getSqlSessionFactory() {return sqlSessionFactory;}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}public static void closeSession(SqlSession sqlSession) {if(sqlSession != null) {sqlSession.close();}}}


3、创建实体类Student.java
public class Student {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "id : " + id + ", name : " + name;}}


3、创—AO访问接口IStudentDAO.java
public interface IStudentDAO {Student selectStudentById(int id);List<Student> selectStudents();void insertStudent(Student student);void updateStudent(Student student);void deleteStudent(int id);}


4、创建访问SQL语句配置文件student.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.carvin.mybatis.study01"><select id="selectStudents" resultType="Student">select * from student </select><select id="selectStudentById" resultType="Student" parameterType="int">select * from student where id=#{id}</select><insert id="insertStudent" parameterType="Student">insert student(name) values (#{name})</insert><update id="updateStudent" parameterType="Student">update student set name=#{name} where id=#{id}</update><delete id="deleteStudentById" parameterType="int">delete from student where id=#{id}</delete></mapper>


5、实现数据库访问接口StudentDAOImpl.java
public class StudentDAOImpl implements IStudentDAO {@Overridepublic void insertStudent(Student student) {SqlSession session = SqlSessionFactoryUtil.getSqlSession();try {session.insert("com.carvin.mybatis.study01.insertStudent", student);} finally {try {session.commit();} catch (Exception e) {session.rollback();e.printStackTrace();}SqlSessionFactoryUtil.closeSession(session);}}@Overridepublic void updateStudent(Student student) {SqlSession session = SqlSessionFactoryUtil.getSqlSession();try {session.update("com.carvin.mybatis.study01.updateStudent", student);} finally {try {session.commit();} catch (Exception e) {session.rollback();e.printStackTrace();}SqlSessionFactoryUtil.closeSession(session);}}@Overridepublic void deleteStudent(int id) {SqlSession session = SqlSessionFactoryUtil.getSqlSession();try {session.delete("com.carvin.mybatis.study01.deleteStudentById", id);} finally {try {session.commit();} catch (Exception e) {session.rollback();e.printStackTrace();}SqlSessionFactoryUtil.closeSession(session);}}@Overridepublic List<Student> selectStudents() {SqlSession session = SqlSessionFactoryUtil.getSqlSession();List<Student> results = null;try {results = session.selectList("com.carvin.mybatis.study01.selectStudent");} finally {SqlSessionFactoryUtil.closeSession(session);}return results;}@Overridepublic Student selectStudentById(int id) {SqlSession session = SqlSessionFactoryUtil.getSqlSession();Student student = null;try {student = (Student) session.selectOne("com.carvin.mybatis.study01.selectStudentById", id);} finally {SqlSessionFactoryUtil.closeSession(session);}return student;}}


6、创建测试类StudentTest.java

public class StudentTest {@Testpublic void testSelectList() {IStudentDAO dao = new StudentDAOImpl();List<Student> results = dao.selectStudents();System.out.println(results.size());}@Testpublic void testSelectOne() {IStudentDAO dao = new StudentDAOImpl();Student student = dao.selectStudentById(2);System.out.println(student);}@Testpublic void testInsert() {Student student = new Student();student.setName("carvin");IStudentDAO dao = new StudentDAOImpl();dao.insertStudent(student);}@Testpublic void testUpdate() {IStudentDAO dao = new StudentDAOImpl();Student student = dao.selectStudentById(2);student.setName("oldsmall");dao.updateStudent(student);}@Testpublic void testDelete() {IStudentDAO dao = new StudentDAOImpl();dao.deleteStudent(2);}}


7、测试

至此,一个简单的MyBatis应用就完成了。

读书人网 >软件架构设计

热点推荐