最近在开发项目中用到maven工具。。纠结得要命,什么也不懂,还要配置很多东西。不过maven这个工具的好处很多。例如不需要导入包,直接引用仓库就OK等等......
好了,回正题 这章介绍了一下将maven+MyBatis+Spring整合在一起使用,
首先看一下结构图吧!思路一定要清晰
一:实体类
package log.model;
import java.util.Date;
public class Log {
//日志ID
private int id;
//日志类型
private String logType;
//操作人账号
private String operatorAccount;
//日志描述
private String Des;
//备注
private String remark;
//日志时间
private Date logTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLogType() {
return logType;
}
public void setLogType(String logType) {
this.logType = logType;
}
public String getOperatorAccount() {
return operatorAccount;
}
public void setOperatorAccount(String operatorAccount) {
this.operatorAccount = operatorAccount;
}
public String getDes() {
return Des;
}
public void setDes(String des) {
Des = des;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getLogTime() {
return logTime;
}
public void setLogTime(Date logTime) {
this.logTime = logTime;
}
}
二:数据访问接口
package log.dao;
import log.util.Paging;
public interface LogDao {
/**
* 插入日志
* @param logType 日志类型
* @param account 操作人账号
* @param logDes 日志描述
* @param remark 备注
*/
public void log(String logType, String account, String logDes, String remark);
/**
* 查询日志
* @param page
* @param logType 日志类型
* @param account 操作人账号
* @param startTime 开始时间
* @param endTime 结束时间
*/
public void getLogs(Paging page, String logType, String account, String startTime, String endTime);
}
三:服务层接口
package log.dao;
import log.util.Paging;
public interface LogService {
/**
* 插入日志
* @param account 用户名
* @param logType 日志类型
* @param logDes 日志描述
* @param remark 备注
*/
public void log(String account, String logType, String logDes, String remark);
/**
* 查询日志
* @param page
* @param logType 日志类型
* @param startTime 开始时间
* @param endTime 结束时间
*/
public void getLogs(Paging page, String logType, String account, String startTime, String endTime);
}
四:工具类
package log.dao;
import javax.servlet.http.HttpServletRequest;
import log.util.Paging;
/**
* WEB工具类方法.
* 用于保存分页数据到HTTP请求等.
*
* @author [email protected]
*
*/
public class WebUtils {
private WebUtils() {
}
}
五:数据访问实现类
package log.impl;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import log.dao.LogDao;
import log.model.Log;
import log.util.Paging;
public class LogDaoImpl extends SqlSessionDaoSupport implements LogDao{
private Log log;
public Log getLog() {
return log;
}
public void setLog(Log log) {
this.log = log;
}
public void log(String logType, String account, String logDes, String remark) {
log.setLogType(logType);
log.setOperatorAccount(account);
log.setDes(logDes);
log.setRemark(remark);
this.getSqlSession().insert("log.add", log);
}
public void getLogs(Paging page, String logType, String account,
String startTime, String endTime) {
page.setData(this.getSqlSession().selectList("log.queryUserByID"));
}
}
六:服务层实现类
package log.impl;
import log.dao.LogDao;
import log.dao.LogService;
import log.util.Paging;
public class LogServiceImpl implements LogService{
private LogDao logDao;
public LogDao getLogDao() {
return logDao;
}
public void setLogDao(LogDao logDao) {
this.logDao = logDao;
}
public void log(String account, String logType, String logDes, String remark) {
// TODO Auto-generated method stub
logDao.log(logType, account, logDes, remark);
}
public void getLogs(Paging page, String logType, String account, String startTime, String endTime) {
// TODO Auto-generated method stub
logDao.getLogs(page, logType, account, startTime, endTime);
}
}
七:数据访问接口映射文件 即是myibatis映射文件 (LogMapper.xml)
ID id,
LOG_TYPE logType,
OPERATOR_ACCOUNT operatorAccount,
LOG_DES,REMARK Des,
LOG_TIME remark
insert into log(ID,LOG_TYPE,OPERATOR_ACCOUNT,LOG_DES,REMARK,LOG_TIME)
values(#{id},#{logType},#{operatorAccount},#{Des},#{remark},#{logTime})
八:mybatis总配置文件 (sqlMapConfig.xml)
九:spring 配置文件
十:jdbc配置文件(spring-jdbc.xml)
十一:junit测试类
package org.clzps.main;
import junit.framework.TestCase;
import log.dao.LogService;
import log.model.Log;
import log.util.Paging;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Unit test for simple App.
*/
public class MybatisSpringTest extends TestCase
{
public MybatisSpringTest( String testName )
{
super( testName );
}
public void testMain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-log-context.xml");
//LogService logService = (LogService)context.getBean("logService");
LogService logService = (LogService)context.getBean(LogService.class);
// Log log = new Log();
Paging page = new Paging();
String logType = null;
String account = null;
String startTime = null;
String endTime = null;
// log.setId(10);
// log.setLogType("杂有");
// log.setOperatorAccount("旭民不鼐太极转运鼠国以");
// log.setRemark("ff d s s ");
// log.setDes("01254221");
// log.setLogTime(new Date());
// logDao.insert(log);
logService.getLogs(page, logType, account, startTime, endTime);
for (Object o : page.getData()) {
Log log = (Log) o;
System.out.println(log.getId());
}
}
}
十二:引用包的配置文件 pom.xml
4.0.0
com.csair.ibe
com.csair.ibe.log
1.0.0
javax.servlet
servlet-api
2.5
provided
junit
junit
4.7
test
org.mybatis
mybatis
3.0.4
jar
compile
org.mybatis
mybatis-spring
1.0.0-RC3
jar
compile
mysql
mysql-connector-java
5.1.16
jar
compile
commons-dbcp
commons-dbcp
1.2.2
jar
compile
commons-collections
commons-collections
3.2.1
jar
compile
net.sourceforge.jtds
jtds
1.2.4
源码:http://download.csdn.net/detail/mmm333zzz/4652815
转载请注明出处
作者:蓝缘
出处:http://blog.csdn.net/mmm333zzz/article/details/8077542