[代码] dao接口
package springmvc.dao;
import java.util.List;
import java.util.Map;
public interface CommonDao<T> {
/**
* 根据id查找唯一对象
* @param mapper
* @param t
* @return
*/
public T findById(String mapper,T t);
/**
* 根据where条件查找唯一对象
* @param mapper
* @param t
* @return
*/
public T findByWhere(String mapper,T t);
/**
* 根据sql语句查找唯一对象
* @param mapper
* @param t
* @return
*/
public T findBySql(String mapper,T t);
/**
* 根据where条件查找对象列表
* @param mapper
* @param t
* @return
*/
public List<T> findListByWhere(String mapper,T t);
/**
* 查找和t某些值相等的对象列表
* @param mapper
* @param t
* @return
*/
public List<T> findListByObj(String mapper,T t);
/**
* 根据sql语句查找对象列表
* @param mapper
* @param t
* @return
*/
public List<T> findListBySql(String mapper,T t);
/**
* 根据sql语句查询唯一字段值
* @param mapper
* @param t
* @return
*/
public Object findValueBySql(String mapper,T t);//
/**
* 根据id更新
* @param mapper
* @param t
*/
public void updateById(String mapper,T t);
/**
* 根据where条件更新
* @param mapper
* @param t
*/
public void deleteByWhere(String mapper,T t);
public void updateByWhere(String mapper,T t);
public void deleteById(String mapper,T t);
//以下方法基本调用sqlsession的相应方法 ,isAuto:是否自动提交
public void insert(String mapper,T t);
public void updateById(String mapper,T t,boolean isAuto);
public void deleteByWhere(String mapper,T t,boolean isAuto);
public void updateByWhere(String mapper,T t,boolean isAuto);
public void deleteById(String mapper,T t,boolean isAuto);
public void insert(String mapper,T t,boolean isAuto);
public T selectOne(String mapper);
public List<T> selectList(String mapper);
public void update(String mapper);
public void delete(String mapper);
public void insert(String mapper);
public Map selectMap(String mapper,String arg1);
public Map selectMap(String mapper,String arg1,String arg2);
public void update(String mapper,boolean isAuto);
public void delete(String mapper,boolean isAuto);
public void insert(String mapper,boolean isAuto);
/**
* 执行sql语句 :增,删,改
* @param mapper
* @param t
*/
public void executeSql(String mapper,T t);
public void commit();
public void close();
}
[代码] dao接口的实现
package springmvc.dao.impl;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springmvc.dao.CommonDao;
@Service("commonDao")
public class CommonDaoImpl<T> implements CommonDao<Object>{
@Autowired
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
private boolean isAuto=true;//默认自动提交
public SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public Object findById(String mapper,Object obj) {
Object o=getSqlSession().selectOne(mapper, obj);
return o;
}
public Object findByWhere(String mapper,Object t) {
List list=getSqlSession().selectList(mapper,t);
if(list!=null&&list.size()>0){
return list.get(0);
}else{
return null;
}
}
public Object findBySql(String mapper,Object t) {
List list=getSqlSession().selectList(mapper,t);
if(list!=null&&list.size()>0){
return list.get(0);
}else{
return null;
}
}
public List findListByWhere(String mapper,Object t) {
List list=getSqlSession().selectList(mapper,t);
return list;
}
public List findListByObj(String mapper,Object t) {
List list=getSqlSession().selectList(mapper,t);
return list;
}
public List findListBySql(String mapper,Object t) {
List list=getSqlSession().selectList(mapper,t);
return list;
}
public Object findValueBySql(String mapper, Object t) {
List list=getSqlSession().selectList(mapper,t);
if(list!=null&&list.size()>0){
return list.get(0);
}else{
return null;
}
}
public Map selectMap(String mapper, String arg1) {
Map map=getSqlSession().selectMap(mapper,arg1);
return map;
}
public Map selectMap(String mapper, String arg1,String arg2) {
Map map=getSqlSession().selectMap(mapper,arg1,arg2);
return map;
}
public void updateById(String mapper, Object t) {
this.getSqlSession().update(mapper, t);
}
public void deleteByWhere(String mapper, Object t) {
this.getSqlSession().update(mapper, t);
}
public void updateById(String mapper, Object t, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().update(mapper, t);
}
public void deleteByWhere(String mapper, Object t, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().update(mapper, t);
}
public void updateByWhere(String mapper, Object t) {
this.setAuto(isAuto);
this.getSqlSession().update(mapper, t);
}
public void deleteById(String mapper, Object t) {
this.getSqlSession().delete(mapper, t);
}
public void updateByWhere(String mapper, Object t, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().update(mapper, t);
}
public void deleteById(String mapper, Object t, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().delete(mapper, t);
}
public void insert(String mapper,Object t) {
getSqlSession().insert(mapper,t);
}
public void insert(String mapper,Object t, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().insert(mapper,t);
}
public Object selectOne(String mapper) {
Object obj=this.getSqlSession().selectOne(mapper);
return obj;
}
public List<Object> selectList(String mapper) {
List list=this.getSqlSession().selectList(mapper);
return list;
}
public void update(String mapper) {
this.getSqlSession().update(mapper);
}
public void delete(String mapper) {
this.getSqlSession().delete(mapper);
}
public void insert(String mapper) {
this.getSqlSession().insert(mapper);
}
public void update(String mapper, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().update(mapper);
}
public void delete(String mapper, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().delete(mapper);
}
public void insert(String mapper, boolean isAuto) {
this.setAuto(isAuto);
this.getSqlSession().insert(mapper);
}
public void executeSql(String mapper,Object t) {
this.getSqlSession().update(mapper, t);
}
public void commit() {
if(sqlSession!=null)
sqlSession.commit();
}
private SqlSession getSqlSession(){
// sqlSession=sqlSession==null?
// sqlSessionFactory.openSession(ExecutorType.BATCH, isAuto):sqlSession; 执行后必须要commit
sqlSession=sqlSession==null? sqlSessionFactory.openSession(isAuto):sqlSession;
return sqlSession;
}
private boolean isAuto() {
return isAuto;
}
private void setAuto(boolean isAuto) {
this.isAuto = isAuto;
}
public void close() {
if(sqlSession!=null)
sqlSession.close();
}
}
[代码] UserMapper.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="springmvc.beans.User"> <resultMap id="UserMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> </resultMap> <select id="getById" parameterType="int" resultMap="UserMap"> select * from user where id=#{id} </select> <select id="getByWhere" parameterType="User" resultMap="UserMap"> select * from user ${where} </select> <select id="getBySql" parameterType="User" resultMap="UserMap"> ${sql} </select> <select id="getByObj" parameterType="User" resultMap="UserMap"> select * from user where 1=1 <if test="id != null and id!= '' "> AND id = #{id} </if> <if test="username != null and username != '' "> AND username = #{username} </if> <if test="password != null and password!= '' "> AND password = #{password} </if> </select> <select id="findListByWhere" parameterType="String" resultMap="UserMap"> select * from user ${where} </select> <select id="findListByObj" parameterType="User" resultMap="UserMap"> select * from user where 1=1 <if test="id != null and id!= '' "> AND id = #{id} </if> <if test="username != null and username != '' "> AND username = #{username} </if> <if test="password != null and password!= '' "> AND password = #{password} </if> </select> <select id="findListBySql" parameterType="User" resultMap="UserMap"> ${sql} </select> <select id="findValueBySql" parameterType="User" resultType="java.lang.Object"> ${sql} </select> <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id" keyColumn="GENERATED_KEY" > insert into user(username,password) values(#{username},#{password}) </insert> <update id="updateById" parameterType="User" > update user set username=#{username},password=#{password} where id=#{id} </update> <update id="updateByWhere" parameterType="String" > update user set username=#{username},password=#{password} ${where} </update> <delete id="deleteById" parameterType="User" > delete from user where id=#{id} </delete> <delete id="deleteByWhere" parameterType="User" > delete from user ${where} </delete> <update id="execute" parameterType="User" > ${sql} </update> </mapper>
[代码] Test.java
package test;
import java.util.List;
import springmvc.beans.User;
import springmvc.dao.impl.CommonDaoImpl;
import util.MyUtils;
public class Test {
private static CommonDaoImpl<User> dao=(CommonDaoImpl<User>) MyUtils.getCtx().getBean("commonDao");
private static String mapper="springmvc.beans.User.@";
public static void getById(int id){
User u=(User)dao.findById(mapper.replace("@", "getById"), id);
System.out.println(u.getUsername());
}
public static void getByWhere(){
User u=new User();
u.setWhere("where id=1");
u=(User)dao.findByWhere(mapper.replace("@", "getByWhere"), u);
System.out.println(u.getUsername());
}
public static void getByObj(){
User u=new User();
u.setId(7);
u=(User)dao.findByWhere(mapper.replace("@", "getByObj"), u);
System.out.println(u.getUsername());
}
public static void getListByObj(){
User u=new User();
List<User> list=dao.findListByObj(mapper.replace("@", "getByObj"), u);
System.out.println(list.size());
}
public static void getBySql(){
User u=new User();
u.setSql("select * from user where id=1");
u=(User)dao.findBySql(mapper.replace("@", "getBySql"), u);
System.out.println(u.getUsername());
}
public static void getListBySql(){
User u=new User();
u.setSql("select * from user");
List<User> list=dao.findListBySql(mapper.replace("@", "getBySql"), u);
System.out.println(list.get(1).getId());
}
public static void getValue(){
User u=new User();
u.setSql("select id from user where username='ahutcb@sina.com'");
Object o=dao.findValueBySql(mapper.replace("@", "findValueBySql"), u);
System.out.println(o);
}
public static void insert(){
User u=new User();
u.setUsername("zs");
u.setPassword("123456");
dao.insert(mapper.replace("@", "insert"), u,true);
// dao.commit();// 设置的为true 为什么还要commmit
System.out.println(u.getId());
}
public static void insert2(){
User u=new User();
u.setUsername("zs");
u.setPassword("12345e6");
MyUtils.getSqlSession(false).insert(mapper.replace("@", "insert"), u);//此处没有commit为什么提交了
System.out.println(u.getId());
}
public static void deleteByWhere(){
User u=new User();
u.setWhere("where password='12345e6'");
dao.deleteByWhere(mapper.replace("@", "deleteByWhere"), u);
}
public static void deleteById(){
User u=new User();
u.setId(16);
dao.deleteByWhere(mapper.replace("@", "deleteById"), u);
}
public static void updateByWhere(){
User u=new User();
u.setWhere("where password='12345e6'");
u=(User) dao.findByWhere(mapper.replace("@", "getByWhere"), u);
u.setWhere("where password='12345e6'");
u.setUsername("432e1");
dao.deleteByWhere(mapper.replace("@", "updateByWhere"), u);
}
public static void updateById(){
User u=new User();
u.setId(10);
u.setPassword("456");
dao.deleteByWhere(mapper.replace("@", "updateById"), u);
}
public static void main(String[]args){
//getById(1);
//getByWhere();
//getBySql();
//getByObj();
//getListByObj();
//getListBySql();
//getValue();
// insert2();
//deleteByWhere();
//deleteById();
//updateById();
updateByWhere();
}
}