目录
- package demo2.service;
-
- import demo1.bean.Student;
- import java.util.List;
-
- public interface StudentService {
- //查询全部
- public abstract List
selectAll(); - //根据id查询
- public abstract Student selectById(Integer sid);
- //新增数据
- public abstract Integer insert(Student stu);
- //修改数据
- public abstract Integer update(Student stu);
- //删除数据
- public abstract Integer delete(Integer sid);
- }
- package demo2.service;
-
- import demo1.bean.Student;
- import demo2.dao.StudentMapper;
- import demo2.dao.StudentMapperImpl;
-
- import java.util.List;
-
- public class StudentServiceImpl implements StudentService{
- //创建持久层对象
- private StudentMapper mapper = new StudentMapperImpl();
-
- @Override
- public List
selectAll() { - return mapper.selectAll();
- }
-
- @Override
- public Student selectById(Integer sid) {
- return mapper.selectById(sid);
- }
-
- @Override
- public Integer insert(Student stu) {
- return mapper.insert(stu);
- }
-
- @Override
- public Integer update(Student stu) {
- return mapper.update(stu);
- }
-
- @Override
- public Integer delete(Integer sid) {
- return mapper.delete(sid);
- }
- }
- package demo2.controller;
-
- import demo1.bean.Student;
- import demo2.service.StudentService;
- import demo2.service.StudentServiceImpl;
- import org.junit.Test;
- import java.util.List;
-
- public class StudentController {
- //创建业务层对象
- private StudentService service = new StudentServiceImpl();
- //查询全部功能测试
- @Test
- public void selectAll(){
- List
students = service.selectAll(); - for(Student stu : students){
- System.out.println(stu);
- }
- }
- //根据id查询功能测试
- @Test
- public void selectById(){
- Student stu = service.selectById(3);
- System.out.println(stu);
- }
- //新增功能测试
- @Test
- public void insert(){
- Student stu = new Student(4,"赵六",26);
- Integer result = service.insert(stu);
- System.out.println(result);
- }
- //修改功能测试
- @Test
- public void update(){
- Student stu = new Student(4,"倪炜豪",26);
- Integer result = service.update(stu);
- System.out.println(result);
- }
- //删除功能测试
- @Test
- public void delete(){
- Integer result = service.delete(4);
- System.out.println(result);
- }
- }
- package demo2.dao;
-
- import demo1.bean.Student;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
-
- //持久层实现类
- public class StudentMapperImpl implements StudentMapper{
- //查询全部
- @Override
- public List
selectAll() { - List
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.执行映射配置文件中的sql语句,并接收结果
- list = sqlSession.selectList("StudentMapper.selectAll");
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //5.释放资源
- if(sqlSession!=null){
- sqlSession.close();
- }
- if(is!=null){
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //6.返回结果
- return list;
- }
- //根据id查询
- @Override
- public Student selectById(Integer sid) {
- Student stu = 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.执行映射配置文件中的sql语句,并接收结果
- stu = sqlSession.selectOne("StudentMapper.selectById",sid);
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //5.释放资源
- if(sqlSession!=null){
- sqlSession.close();
- }
- if(is!=null){
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //6.返回结果
- return stu;
- }
- //新增数据
- @Override
- public Integer insert(Student stu) {
- Integer result = 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.执行映射配置文件中的sql语句,并接收结果
- result = sqlSession.insert("StudentMapper.insert",stu);
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //5.释放资源
- if(sqlSession!=null){
- sqlSession.close();
- }
- if(is!=null){
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //6.返回结果
- return result;
- }
- //修改数据
- @Override
- public Integer update(Student stu) {
- Integer result = 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.执行映射配置文件中的sql语句,并接收结果
- result = sqlSession.update("StudentMapper.update",stu);
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //5.释放资源
- if(sqlSession!=null){
- sqlSession.close();
- }
- if(is!=null){
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //6.返回结果
- return result;
- }
- //删除数据
- @Override
- public Integer delete(Integer sid) {
- Integer result = 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.执行映射配置文件中的sql语句,并接收结果
- result = sqlSession.delete("StudentMapper.delete",sid);
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //5.释放资源
- if(sqlSession!=null){
- sqlSession.close();
- }
- if(is!=null){
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //6.返回结果
- return result;
- }
- }
- <settings>
- <setting name="logImpl" value="log4j"/>
- settings>