Maven整合Spring3.X+Mybatis3.X

阅读更多

网上有很多整合的例子,但大多不能跑起来,至此,本人整理了一套基本的搭建.以备不时之需.

 

首先Spring3.X 整合 Mybatis3.X 有些jar稍微有变化,大家注意!!! 附件是所有内容,下载测试

 

先上整理目录结构


Maven整合Spring3.X+Mybatis3.X_第1张图片
 目录内容不多言,对maven不太了解的童靴自行学习.

 

pom.xml的内容


	4.0.0
	spring.mybatis.maven
	spring-mybatis-maven
	war
	0.0.1-SNAPSHOT
	sm.maven Maven Webapp
	http://maven.apache.org

	
		3.0.5.RELEASE
	

	
		
			junit
			junit
			4.10
			test
		



		
			aopalliance
			aopalliance
			1.0
			true
		

		
		
			org.mybatis
			mybatis
			3.2.3
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		

		
		
			javax.servlet
			servlet-api
			2.5
			provided
		
		
			javax.servlet.jsp
			jsp-api
			2.1
			provided
		

		
		
			mysql
			mysql-connector-java
			5.1.18
		
		
            
		
			org.springframework
			spring-aop
			${org.springframework.version}
			true
		
		
			org.springframework
			spring-beans
			${org.springframework.version}
			compile
		
		
			org.springframework
			spring-context
			${org.springframework.version}
			true
		
		
			org.springframework
			spring-core
			${org.springframework.version}
			compile
		
		
			org.springframework
			spring-jdbc
			${org.springframework.version}
			compile
		
		
			org.springframework
			spring-tx
			${org.springframework.version}
			compile
		
		
			org.springframework
			spring-web
			${org.springframework.version}
			true
		

		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
			commons-pool
			commons-pool
			1.5.3
			test
		
		
			commons-collections
			commons-collections
			3.2.1
		

		
		
			org.slf4j
			slf4j-api
			1.7.5
		
		
			org.slf4j
			slf4j-log4j12
			1.7.5
		

		
			dom4j
			dom4j
			1.6.1
		

		
			javassist
			javassist
			3.3
		
		
			org.aspectj
			aspectjweaver
			1.7.4
		

	
	
		sm.maven
		
    
        
            .
            src/main/config
        
    



 

IUserDAO 演示几个简单的方法

package org.xyz.dao;

import java.util.List;
import org.springframework.dao.DataAccessException;
import org.xyz.po.User;
/*DataAccessException 异常是Spring整合DAO层的顶级异常*/

public interface IUserDAO {
	
	public List getAllUser() throws DataAccessException;
	
	public User getUserById(Integer id)throws DataAccessException;
	
	public void deleteUserById(Integer id) throws DataAccessException;
	
	public void modifyUserById(User user) throws DataAccessException;
	
	/*保存,返回主键id*/
	public Integer saveUser(User user) throws DataAccessException;
}

 
 DAO的实现

package org.xyz.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.xyz.po.User;

public class UserMapperImpl implements IUserDAO {

	final static Logger  log = Logger.getLogger(UserMapperImpl.class);
	
	private SqlSession sqlSession;
	
	public SqlSession getSqlSession() {
		return sqlSession;
	}

	public void setSqlSession(SqlSession sqlSession) {
		log.warn("初始化sqlSession success ! "+sqlSession);
		this.sqlSession = sqlSession;
	}

	public User getUserById(Integer id)  throws DataAccessException{
		log.debug("-----------------delete user success ! --size:"+id);
		return sqlSession.selectOne("org.xyz.po.UserMapper.getUser",id );
	}

	public void deleteUserById(Integer id) throws DataAccessException{
		sqlSession.delete("org.xyz.po.UserMapper.deleteUser", id);
		log.debug("-----------------delete user success ! --"+id);
		
	}

	public void modifyUserById(User user)throws DataAccessException {
		sqlSession.update("org.xyz.po.UserMapper.modifyUser", user);
		log.debug("------------------modify user success ! --"+user);
		
	}

	public Integer saveUser(User user)throws DataAccessException {
		log.debug("delete user success ! --"+user);
		return sqlSession.insert("org.xyz.po.UserMapper.saveUser", user);
		
	}

	public List getAllUser() throws DataAccessException{
		return sqlSession.selectList("org.xyz.po.UserMapper.getAllUser");
	}

}

自定义异常

 

package org.xyz.exception;

import org.springframework.dao.DataAccessException;

/**
 * 自定义异常
 */
public class ServiceException extends DataAccessException {

	public ServiceException(String msg) {
		super(msg);
	}

	@Override
	public String getMessage() {

		return super.getMessage();
	}

	@Override
	public Throwable getMostSpecificCause() {
		return super.getMostSpecificCause();
	}

	@Override
	public Throwable getRootCause() {
		return super.getRootCause();
	}

	private static final long serialVersionUID = 6748277402450587224L;

}

 

User 实体

package org.xyz.po;

import java.io.Serializable;
import java.sql.Date;

public class User implements Serializable {

	private static final long serialVersionUID = 9113088589369627813L;
	private Integer id;
	private String name;
	private String password;
	private Date lastLogintime;
	private boolean isLogin;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public Date getlastLoginTime() {
		return lastLogintime;
	}
	public void setlastLogintime(Date lastLogintime) {
		this.lastLogintime = lastLogintime;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public boolean isLogin() {
		return isLogin;
	}
	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}
	
	
	@Override
	public String toString() {
		return this.name+","+this.id+","+this.password;
	}
	
}

 

Service层接口

package org.xyz.service;
import java.util.List;
import org.xyz.exception.ServiceException;
import org.xyz.po.User;
/*Service层 用来测试事务,本例没有实际意义*/
public interface IUserService {
	
	/*修改用户信息*/
	public void updateAndSave(User user) throws ServiceException ;
	
	/*查询所有用户信息*/
	public List getAllUser() throws ServiceException;
	
	/*查询用户信息*/
	public User getUserById(Integer id ) throws ServiceException;
	
}

 

Service实现

package org.xyz.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.xyz.dao.IUserDAO;
import org.xyz.exception.ServiceException;
import org.xyz.po.User;

public class UserServiceImpl implements IUserService {
	static final Logger log = Logger.getLogger(UserServiceImpl.class);

	private IUserDAO userDAO;

	/* 用update来测试事务 */
	public void updateAndSave(User user) throws ServiceException {
		try {
			userDAO.saveUser(user);
			// 模拟异常
			String s = null;
			System.out.println(s.length());
			/*
			 * 通过捕获Service层方法的DataAccessException来提交和回滚事务的,
			 * 而Service层方法的DataAccessException又是来自调用DAO层方法所产生的异常
			 * 在Service层我们可以自己捕获DAO方法所产成的DataAccessException,
                         * 然后再抛出一个业务方法有意义的异常
			 */

			user.setlastLogintime(
					new java.sql.Date(System.currentTimeMillis()));
			log.info("updateAndSave ... updateUser() ... ");
			
			userDAO.modifyUserById(user);
		} catch (Exception e) {
			throw new ServiceException("update and save fail ");
		}
	}

	public User getUserById(Integer id) throws ServiceException {
		try {
			return userDAO.getUserById(id);
		} catch (Exception e) {
			throw new ServiceException("getUserById fail ");
		}
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public List getAllUser() {

		return userDAO.getAllUser();
	}

}

 

测试

package xyz.test;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xyz.dao.IUserDAO;
import org.xyz.po.User;

public class Test1 {
	private static final Logger log = Logger.getLogger(Test1.class);
	@Test
	public void f1(){
		String CONFIG = "applicationContext-1.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(CONFIG);
		IUserDAO UserDao = (IUserDAO) ac.getBean("UserDao");
		User user = UserDao.getUserById(1);
		System.out.println(user.getName());
		log.info(user.getName());
	}
}

 

至此 代码部分完成,接下来看配置文件.log4j.properties. jdbc.properties就不列举出来,google很多

 

applicationContext-1的配置内容





	
	

	
	
		
		
		
		
	
	
       
	
		
		   
		 
	


	
			  
	 
	

	
	
		
		

	
		
	
	
	
	
		
	
	
	
		
		
			
			
			
			
		
	
	
	
	
		
		
		
	

 

mybatis-config.xml






  
       
 
    
        
    

 

UserMapper.xml




	
		
		
		
		
		
	
	
	
	
	
		
	
	
	
	 	delete from sm_user where  id=#{id}  
	
	
	
	
		update sm_user set nickname =#{name},
			password = #{password} , lastLogintime = #{lastLogintime},
				isLogin = #{isLogin} 
			id = #{id}
	
	
	
	
		insert into sm_user (nickname,password,lastLogintime,isLogin)
				values (#{name},#{password},#{lastLoginTime},#{isLogin})
	
	



 

全部完成.

执行maven test 即可

 

  • Maven整合Spring3.X+Mybatis3.X_第2张图片
  • 大小: 11.2 KB
  • sm-maven.jar (24.8 KB)
  • 下载次数: 2
  • 查看图片附件

你可能感兴趣的:(maven,Spring3.x,mybatis3.x,整合)