先导入MyBatis的jar包。项目依旧使用控制层——>业务层——>持久层的结构,使用MyBatis进行数据库CRUD。整体结构如下:
MyBatisConfig.xml
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<typeAlias type="com.example.bean.Student" alias="student"/>
<package name="com.example.bean"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC">transactionManager>
<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="StudentMapper.xml"/>
mappers>
configuration>
StudentMapper.xml
<mapper namespace="StudentMapper">
<select id="selectAll" resultType="student">
SELECT * FROM student
select>
<select id="selectById" resultType="student" parameterType="int">
SELECT * FROM student WHERE sid=#{sid}
select>
<insert id="insert" parameterType="student">
INSERT INTO student VALUES(#{sid}, #{name}, #{age}, #{birthday});
insert>
<update id="update" parameterType="student">
UPDATE student SET name=#{name}, age=#{age}, birthday=#{birthday} WHERE sid=#{sid}
update>
<delete id="delete" parameterType="java.lang.Integer">
DELETE FROM student WHERE sid=#{sid};
delete>
mapper>
定义接口:
package com.example.mapper;
import com.example.bean.Student;
import java.util.List;
//持久层接口
public interface StudentMapper {
//查询全部
public abstract List<Student> selectAll();
//根据id查询
public abstract Student selectById(Integer id);
//新增数据
public abstract Integer insert(Student student);
//修改数据
public abstract Integer update(Student student);
//根据id删除数据
public abstract Integer delete(Integer id);
}
接口实现:
public class StudentMapperImpl implements StudentMapper {
//查询全部
@Override
public List<Student> selectAll() {
InputStream is = null;
SqlSession sqlSession = null;
List<Student> list = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MybatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//4.执行映射配置文件中的SQL语句,并接收结果
list = sqlSession.selectList("StudentMapper.selectAll");
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放资源
if(sqlSession != null)
sqlSession.close();
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
//根据Id查询
@Override
public Student selectById(Integer id) {
InputStream is = null;
SqlSession sqlSession = null;
Student stu = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MybatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//4.执行映射配置文件中的SQL语句,并接收结果
stu = sqlSession.selectOne("StudentMapper.selectById", id);
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放资源
if(sqlSession != null)
sqlSession.close();
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stu;
}
//插入一条数据
@Override
public Integer insert(Student student) {
InputStream is = null;
SqlSession sqlSession = null;
Integer res = 0;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MybatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//4.执行映射配置文件中的SQL语句,并接收结果
res = sqlSession.insert("StudentMapper.insert", student);
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放资源
if(sqlSession != null)
sqlSession.close();
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
//修改一条数据
@Override
public Integer update(Student student) {
InputStream is = null;
SqlSession sqlSession = null;
Integer res = 0;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MybatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//4.执行映射配置文件中的SQL语句,并接收结果
res = sqlSession.update("StudentMapper.update", student);
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放资源
if(sqlSession != null)
sqlSession.close();
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
@Override
public Integer delete(Integer id) {
InputStream is = null;
SqlSession sqlSession = null;
Integer res = 0;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MybatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
//4.执行映射配置文件中的SQL语句,并接收结果
res = sqlSession.delete("StudentMapper.delete", id);
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放资源
if(sqlSession != null)
sqlSession.close();
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
}
定义接口:
package com.example.service;
import com.example.bean.Student;
import java.util.List;
//业务层接口
public interface StudentService {
//查询全部
public abstract List<Student> selectAll();
//根据id查询
public abstract Student selectById(Integer id);
//新增数据
public abstract Integer insert(Student student);
//修改数据
public abstract Integer update(Student student);
//根据id删除数据
public abstract Integer delete(Integer id);
}
实现接口:
public class StudentServiceImpl implements StudentService {
private StudentMapper mapper = new StudentMapperImpl();//创建持久层对象
@Override
public List<Student> selectAll() {
return mapper.selectAll();
}
@Override
public Student selectById(Integer id) {
return mapper.selectById(id);
}
@Override
public Integer insert(Student student) {
return mapper.insert(student);
}
@Override
public Integer update(Student student) {
return mapper.update(student);
}
@Override
public Integer delete(Integer id) {
return mapper.delete(id);
}
}
public class StudentController {
//创建业务层对象
private StudentService service = new StudentServiceImpl();
//日期格式化
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//查询全部功能测试
@Test
public void selectAll(){
List<Student> students = service.selectAll();
for (Student stu : students) System.out.println(stu);
}
//通过id查询
@Test
public void selectById(){
Student student = service.selectById(3);
System.out.println(student);
}
//新增功能
@Test
public void insert() throws ParseException {
Student student = new Student(5,"小A",35, dateFormat.parse("2010-10-10"));
Integer res = service.insert(student);
System.out.println(res);
}
//修改功能
@Test
public void update() throws ParseException {
Student student = new Student(5,"大A",33,dateFormat.parse("2000-01-01"));
Integer res = service.update(student);
System.out.println(res);
}
//删除功能
@Test
public void delete(){
Integer res = service.delete(5);
System.out.println(res);
}
}
分析:
和传统Dao实现方式基本相同,只需要修改映射配置文件中的名称空间等上面四步即可。现在不再需要StudentMapper的实现类,其方法实现将转移到StudentServiceImpl中进行实现。项目架构如下:
StudentServiceImpl.class
public class StudentServiceImpl implements StudentService {
@Override
public List<Student> selectAll() {
List<Student> list = null;
SqlSession sqlSession = null;
InputStream is = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取到SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取到SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);//等同于:StudentMapper studentMapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
list = studentMapper.selectAll();
} catch (IOException e) {
e.printStackTrace();
}finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return list;
}
@Override
public Student selectById(Integer id) {
Student student = null;
SqlSession sqlSession = null;
InputStream is = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取到SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取到SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);//等同于:StudentMapper studentMapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
student = studentMapper.selectById(id);
} catch (IOException e) {
e.printStackTrace();
}finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return student;
}
@Override
public Integer insert(Student student) {
Integer res = 0;
SqlSession sqlSession = null;
InputStream is = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取到SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取到SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);//等同于:StudentMapper studentMapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
res = studentMapper.insert(student);
} catch (IOException e) {
e.printStackTrace();
}finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return res;
}
@Override
public Integer update(Student student) {
Integer res = 0;
SqlSession sqlSession = null;
InputStream is = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取到SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取到SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);//等同于:StudentMapper studentMapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
res = studentMapper.update(student);
} catch (IOException e) {
e.printStackTrace();
}finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return res;
}
@Override
public Integer delete(Integer id) {
Integer res = 0;
SqlSession sqlSession = null;
InputStream is = null;
try {
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取到SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取到SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);//等同于:StudentMapper studentMapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
res = studentMapper.delete(id);
} catch (IOException e) {
e.printStackTrace();
}finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return res;
}
}