Fork me on GitHub

Dao层

Dao实现类用xml配置代理,Dao与xml放在同一包下,文件名、namespace、id要一一对应。

service接口与实现类为委托类,仅写业务代码,在main中用动态代理来处理事务等。

当需要在service中增加一个方法时,依次添加service接口-->service实现类-->Dao接口-->xml

 

 

//PetDao.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.WebTest01.dao.PetDao"> <select id="getByName" parameterType="java.lang.String" resultType="com.WebTest01.domain.Pet"> select * from pet where name = #{name} </select> <select id="getAll" resultType="com.WebTest01.domain.Pet"> select * from pet </select> <insert id="insertPet"> insert into pet(name,age,owner) values(#{name},#{age},#{owner}) </insert> <update id="updatePet"> update pet set name = #{name}, owner = #{owner} where age = #{age} </update> <delete id="deletePet"> delete from pet where age = #{age} </delete> </mapper>

 

//PetDao.java

package com.WebTest01.dao;

import com.WebTest01.domain.Pet;

import java.util.List;

public interface PetDao {

    public Pet getByName(String name);

    public List<Pet> getAll();

    public void insertPet(Pet pet);

    public void updatePet(Pet pet);

    public void deletePet(Pet pet);

}
//PetService.java

package com.WebTest01.service;

import com.WebTest01.domain.Pet;

import java.util.List;

public interface PetService {

    public Pet getByName(String name);

    public List<Pet> getAll();

    public void insertPet(Pet pet);

    public void updatePet(Pet pet);

    public void deletePet(Pet pet);

}
//PetServiceImpl.java

package com.WebTest01.service.Impl;

import com.WebTest01.dao.PetDao;
import com.WebTest01.domain.Pet;
import com.WebTest01.service.PetService;
import com.WebTest01.util.SqlSessionUtil;

import java.util.List;

public class PetServiceImpl implements PetService {
    //mybatis动态代理创建PetDao实现类
    private PetDao petDao = SqlSessionUtil.getSession().getMapper(PetDao.class);

    @Override
    public Pet getByName(String name) {
        return petDao.getByName(name);
    }

    @Override
    public List<Pet> getAll() {
        return petDao.getAll();
    }

    @Override
    public void insertPet(Pet pet) {
        petDao.insertPet(pet);
    }

    @Override
    public void updatePet(Pet pet) {
        petDao.updatePet(pet);
    }

    @Override
    public void deletePet(Pet pet) {
        petDao.deletePet(pet);
    }
}
//Test

package com.WebTest01.test;

import com.WebTest01.domain.Pet;
import com.WebTest01.service.Impl.PetServiceImpl;
import com.WebTest01.service.PetService;
import com.WebTest01.util.ServiceFactory;

public class Test01 {
    public static void main(String[] args) {
        PetService petService = (PetService) ServiceFactory.getService(new PetServiceImpl());
        Pet pet = new Pet("ttt", "S", 9999);
        petService.deletePet(pet);
    }
}
//ServiceFactory.java

package com.WebTest01.util;

public class ServiceFactory {
    //创建代理service对象
    public static Object getService(Object service){
        return new TransactionInvocationHandler(service).getProxy();
    }

}
//SqlSessionUtil.java
package com.WebTest01.util;

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;

public class SqlSessionUtil {

private SqlSessionUtil(){}

private static SqlSessionFactory sqlSessionFactory = null;

static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}

private static ThreadLocal<SqlSession> t = new ThreadLocal<>();

public static SqlSession getSession(){
SqlSession sqlSession = t.get();
if(sqlSession==null){
sqlSession = sqlSessionFactory.openSession();
t.set(sqlSession);
}
return sqlSession;
}

public static void closeSession(SqlSession sqlSession){
if(sqlSession!=null){
sqlSession.close();
t.remove();
}
}


}
 

 

 

//TransactionInvocationHandler.java

package com.WebTest01.util;

import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TransactionInvocationHandler implements InvocationHandler {

    private Object target;

    public TransactionInvocationHandler(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //return值
        Object obj = null;
        SqlSession  session = null;

        try {
            session = SqlSessionUtil.getSession();
            obj = method.invoke(target, args);
            session.commit();
        } catch (Exception e) {
            if (session != null) {
                session.rollback();
            }
            e.printStackTrace();
        } finally {
            SqlSessionUtil.closeSession(session);
        }
        return obj;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), this);
    }

}

 

posted @ 2020-07-17 10:58  Faded828x  阅读(229)  评论(0编辑  收藏  举报