一、基本步骤(idea跳过)
- 创建maven web项目(打包方式war包)
- 生成部署描述文件 web.xml(全注解方式不需要)
- 修改项目编码为utf-8(假如工作区编码为UTF-8,则无需修改)
- 设置项目targeted runtimes (Tomcat):暂时不用tomcat插件
- 修改项目project facets (jdk1.8)
二、添加项目依赖
- 添加Druid 连接池(两个依赖:mysql驱动,druid库文件)
- 添加Mybatis 框架(三个依赖:mybatis,mybatis-spring,spring-jdbc)
- 添加Spring MVC 模块环境(一个依赖spring-webmvc)
- 添加 jackson库(用于将对象转换为json串)
- 添加Junit 单元测试依赖
- 添加mysql-connector
- log4j2
- servlet api
三、创建配置文件
1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="yhmis" version="2.5">
<display-name>jtsysProject</display-name>
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-configs.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2 spring-configs.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<import resource="classpath:spring-repository.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-web.xml"/>
</beans>
3 spring-repository.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<util:properties id="cfg" location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close" lazy-init="false">
<property name="DriverClassName" value="#{cfg.jdbcDriver}"/>
<property name="Url" value="#{cfg.jdbcUrl}"/>
<property name="Username" value="#{cfg.jdbcUser}"/>
<property name="Password" value="#{cfg.jdbcPassword}"/>
</bean>
<!-- 借助此Bean对象创建SqlSessionFactory对象
,当我们调用工厂对象的getBean方法获取id为sqlSessionFactory
的对象时,系统会返回SqlSessionFactoryBean对象的getObject
方法返回的对象。-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="DataSource" ref="dataSource"/>
<!-- 设置mapper文件的位置 -->
<property name="MapperLocations"
value="classpath*:mapper/sys/*.xml"/>
</bean>
<bean id="daoScanner"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="BasePackage" value="com.jt.**.dao"/>
<!-- 当spring容器只有一个sqlSessionFactory时如下配置可以不写 -->
<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
4 spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.jt.sys.service"/>
</beans>
5 spring-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.jt.sys.controller"/>
<!--启用mvc默认配置(内置一部分bean对象的定义)-->
<mvc:annotation-driven/>
<!--注册视图解析对象 -->
<bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="Prefix" value="/WEB-INF/pages/"/>
<property name="Suffix" value=".html"/>
</bean>
</beans>
6 db.properties
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///jtsys?characterEncoding=utf-8&serverTimezone=UTC
jdbcUser=root
jdbcPassword=root
7 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/strutslog1.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="2"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="WAN"/>
<Logger name="org.apache.struts2" level="WAN"/>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
8 SysLogMapper.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.jt.sys.dao.SysLogDao">
<delete id="deleteObjects">
delete from sys_logs
where id in <!-- (1,2,3,4,5) -->
<foreach collection="ids"
open="("
close=")"
separator=","
item="id">
#{id}
</foreach>
</delete>
</mapper>
四、测试类
1 TestBase
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBase {
protected ClassPathXmlApplicationContext ctx;
@Before
public void init() {
ctx=new ClassPathXmlApplicationContext("spring-configs.xml");
}
@Test
public void testCtx() {
System.out.println(ctx);
}
@After
public void destory() {
ctx.close();
}
}
2 TestDataSource
import org.junit.Test;
import javax.sql.DataSource;
public class TestDataSource extends TestBase{
@Test
public void testDruidDataSource()throws Exception {
DataSource ds=ctx.getBean("dataSource", DataSource.class);
System.out.println(ds.getConnection());
}
}
3 TestMybatis
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
public class TestMyBatis extends TestBase{
@Test
public void testSqlSessionFactory()throws Exception {
SqlSessionFactory ssf=
ctx.getBean("sqlSessionFactory", SqlSessionFactory.class);
System.out.println(ssf);
}
}
4 TestSysLogDao
import java.util.List;
import org.junit.Test;
import com.jt.sys.dao.SysLogDao;
public class TestSysLogDao extends TestBase {
@Test
public void testDeleteObjects() {
SysLogDao dao=
ctx.getBean("sysLogDao",SysLogDao.class);
int rows=dao.deleteObjects(9,10);
System.out.println("delete ok,rows="+rows);
}
}
5 controller和dao
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public class SysLogController {
@RequestMapping("doIndexUI")
public String doIndexUI(){
return "starter";
}
}
import java.util.List;
import org.apache.ibatis.annotations.Param;
/**
* spring-respository.xml 此
* 文件中定义了一个MapperScannerConfigurer
* 对象,此对象会对BasePackage属性指定的包
* 下接口进行扫描,然后会为接口创建实现类的对象,
* 并将这个对象存储到bean池,其key为接口名(首字母小写).
*/
public interface SysLogDao {//SysLogDao.class
/**
* 执行日志删除操作
* @param id
* @return
*/
int deleteObjects(@Param("ids")Integer... id);
}