MyBatis3 入门学习--简单CRUD应用
一、简介:什么是MyBatis?
MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除
了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML
或注解用于配置和原始映射,将接口和Java的POJOs(PlanOldJavaObjects,普通的Java
对象)映射成数据库中的记录。
二、环境介绍
MyBatis3.0.3+MySQL5.5.8
数据库表:student
idnameintvarchar(100)
三、应用开始(我这以学生为例)
1、配置数据库连接配置文件(我的文件放在Classpath下)
Configuration.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> <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.Driver url=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8 username=root password=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; } @Override public String toString() { return "id : " + id + ", name : " + name; } }
3、创建DAO访问接口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 { @Override public 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); } } @Override public 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); } } @Override public 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); } } @Override public 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; } @Override public 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 { @Test public void testSelectList() { IStudentDAO dao = new StudentDAOImpl(); List<Student> results = dao.selectStudents(); System.out.println(results.size()); } @Test public void testSelectOne() { IStudentDAO dao = new StudentDAOImpl(); Student student = dao.selectStudentById(2); System.out.println(student); } @Test public void testInsert() { Student student = new Student(); student.setName("carvin"); IStudentDAO dao = new StudentDAOImpl(); dao.insertStudent(student); } @Test public void testUpdate() { IStudentDAO dao = new StudentDAOImpl(); Student student = dao.selectStudentById(2); student.setName("oldsmall"); dao.updateStudent(student); } @Test public void testDelete() { IStudentDAO dao = new StudentDAOImpl(); dao.deleteStudent(2); } }
7、测试
至此,一个简单的MyBatis应用就完成了。
相关推荐
liqiancao 2020-07-26
chenjiazhu 2020-07-08
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25