最近在开发项目中用到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工具类方法. <br> * 用于保存分页数据到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)
<?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="log"> <sql id="logSelectId"> ID id, LOG_TYPE logType, OPERATOR_ACCOUNT operatorAccount, LOG_DES,REMARK Des, LOG_TIME remark </sql> <select id="queryUserByID" resultType="Log"> SELECT <include refid="logSelectId"/> FROM log </select> <insert id="add" parameterType="Log"> insert into log(ID,LOG_TYPE,OPERATOR_ACCOUNT,LOG_DES,REMARK,LOG_TIME) values(#{id},#{logType},#{operatorAccount},#{Des},#{remark},#{logTime}) </insert> </mapper>
八:mybatis总配置文件 (sqlMapConfig.xml)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="Log" type="log.model.Log" /> </typeAliases> <mappers> <mapper resource="ibatis/LogMapper.xml"/> </mappers> </configuration>
九:spring 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 接连池 --> <import resource="spring-jdbc.xml"/> <!-- <bean id="LogDao" class="log.impl.LogDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="ibatis/sqlMapConfig.xml" /> </bean> --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="ibatis/sqlMapConfig.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="log.dao.LogDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 以下为 依赖注入的 类--> <bean id="log" class="log.model.Log" autowire="byName" /> <bean id="logDao" class="log.impl.LogDaoImpl" autowire="byName" /> <bean id="logService" class="log.impl.LogServiceImpl" autowire="byName"/> </beans>
十:jdbc配置文件(spring-jdbc.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/> <property name="url" value="jdbc:jtds:sqlserver://localhost:1433;databaseName=maven" /> <property name="username" value="sa" /> <property name="password" value="123456789" /> </bean> </beans>
十一: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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.csair.ibe</groupId> <artifactId>com.csair.ibe.log</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.0.4</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.0.0-RC3</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.16</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.2.4</version> </dependency> </dependencies> </project>
源码:http://download.csdn.net/detail/mmm333zzz/4652815
转载请注明出处
作者:蓝缘
出处:http://blog.csdn.net/mmm333zzz/article/details/8077542