Maven-SSM 配置文件
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.woniu</groupId> <artifactId>bkeeping</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <srping.version>4.2.1.RELEASE</srping.version> <mybatis.version>3.3.0</mybatis.version> <slf4j.version>1.7.12</slf4j.version> <log4j.version>1.2.17</log4j.version> <jackson.version>2.7.0</jackson.version> </properties> <dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- spring框架包 start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${srping.version}</version> </dependency> <!-- spring框架包 end --> <!-- mybatis框架包 start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mybatis框架包 end --> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- C3P0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.1.RELEASE</version> </dependency> </dependencies> <build> <plugins> <!-- 配置jdk版本 1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 配置tomcat7 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project>
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="WebApp_ID" version="2.5"> <display-name>bkeeping</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--1.配置监听器,读取contextConfigLocation中的xml文件,若不指定, 默认读WEB-INF路径下的application.xml文件 --> <!--理解:监听器读取到location路径下的xml文件并产生WebApplicationContext对象,将该对象放置在ServletContext的属性里, 这样我们只要得到了Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2.指定spring配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-*.xml</param-value> </context-param> <!--3. 配置DispatcherServlet --> <!--DispatcherServlet的职责: 1、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析; 2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器); 3、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器); 4、通过ViewResolver解析逻辑视图名到具体视图实现; 5、本地化解析; 6、渲染具体的视图等; 7、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。 --> <servlet> <!--3.1 配置的Dispatcher Servlet名称及Class类 --> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--3.2 如若不配置init-param,DispatcherServlet默认使用WebApplicationContext作为上下文, Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml” --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!-- 3.3 启动容器时初始化该Servlet --> <load-on-startup>1</load-on-startup> </servlet> <!--3.4 拦截匹配的请求,Servlet拦截匹配规则 a: *.do 【代表访问以.do结尾的请求由DispatcherServlet解析】 b: / 【代表所有访问都被DispatcherServlet解析,对于不需要解析的静态文件 如:js css 需要配置不让DispatcherServlet 进行解析,用于RESTFUL风格的url,用这个就要配置 InternalResourceViewResolver视图解析器,定义前缀后缀】--> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 4.characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--4.1要使用的字符集,一般我们使用UTF-8(保险起见UTF-8最好)--> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <!--4.2是否强制设置request的编码为encoding,默认false,不建议更改--> <param-name>forceRequestEncoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--4.3是否强制设置response的编码为encoding,建议设置为true--> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <!--4.4 这里不能留空或者直接写 ' / ' ,否则不起作用--> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!--5.restful风格处理静态资源 使用专门的静态资源处理Servlet进行处理-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping> </web-app>
spring-mvc.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!--1.组件扫描 元素会扫描指定的包及其所有的子包,并查找出能够自动注册为Spring Bean的类 类要有 @Component @Controller @Service @Repository--> <context:component-scan base-package="com.woniu.controller"/> <!--2.注解驱动 会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter、ExceptionHandlerExceptionResolver 并支持 支持使用 ConversionService实例对表单参数进行类型转换 支持使用@NumberFormat annotation、@DateTimeFormat注解完成数据类型的格式化 支持使用@Valid注解对JavaBean实例进行JSR303验证 支持使用@RequestBody和@ResponseBody注解 --> <mvc:annotation-driven/> <!--3. 内部资源视图解析器 是UrlBasedViewResolver的子类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"></property> <property name="prefix" value="/WEB-INF/page/"></property> </bean> <!--4.静态资源的访问配置,文件夹配置,二选一 --> <mvc:resources location="/js/" mapping="/js/**" cache-period="31556926"/> <!--4.静态资源的访问配置,文件夹配置,二选一 --> <!-- <mvc:default-servlet-handler/> --> </beans>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bookkeeping jdbc.username=root jdbc.password=123
spring-db.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.指定properties配置文件 的路径 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源,使用Spring内置数据源连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> </beans>
spring-mybatis.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1.在测试的dao时候使用 用getBean的时候需要 如果是用spring注入的不需要 --> <!-- <import resource="spring-db.xml"/> --> <!--2. 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 2.1指定MyBatis主配置文件的路径 如果没配就不用写了,这里我将别名和映射文件所在的包都配了就不用写这个了--> <!-- <property name="configLocation" value="classpath:mybatisconfig.xml" /> --> <!-- 2.2 配置数据源属性 在spring-db总已经配置了数据源 --> <property name="dataSource" ref="dataSource" /> <!--2.3 指定mapper.xml的位置,不设置则默认和dao.class在同一个包下 --> <property name="mapperLocations" value="classpath:com/woniu/mapper/*.xml"/> <!-- 2.4 为实体类使用别名 方便sql语句 --> <property name="typeAliasesPackage" value="com.woniu.pojo"/> <!-- 2.5使用mybatis的插件pagehelper,记得要导依赖 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql supportMethodsArguments=true <!-- 定义当前页和分页大小,多个用逗号分割 --> params=pageNum=currentPage,pageSize=pageSize </value> </property> </bean> </array> </property> </bean> <!-- 3.指定包批量扫描,从dao包中扫描出Mapper接口,自动创建代理对象并且在spring容器中注册 遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录中,mapper中的 namespace是dao的全限定命名--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.woniu.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" /> </bean> </beans>
spring-service.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.组件扫描 @Service 进行自动注册Spring Bean --> <context:component-scan base-package="com.woniu.service" /> </beans>
log4j.properties
#USE THIS SETTING FOR OUTPUT MYBATIS`s SQL ON THE CONSOLE ## this need write where you want to use log4j log4j.logger.com.woniu.dao=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%p] %m [%t] %c [%l]%n #log4j.logger.java.sql.ResultSet=INFO #log4j.logger.org.apache=INFO #log4j.logger.java.sql.Connection=DEBUG #log4j.logger.java.sql.Statement=DEBUG #log4j.logger.java.sql.PreparedStatement=DEBUG
dao测试数据
BillsMapper.java
1 package com.woniu.dao; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Param; 6 7 import com.woniu.pojo.Bills; 8 import com.woniu.vo.BillsVO; 9 10 public interface BillsMapper { 11 public List<Bills> selectBillsList( 12 @Param("currentPage")Integer currentPage,@Param("pageSize")Integer pageSize,@Param("billsVO")BillsVO billsVO); 13 14 }
BillsMapper.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.woniu.dao.BillsMapper"> <select id="selectBillsList" resultMap="listmap"> select *,b.id bid,t.id tid from bills b,bill_type t where b.type_id = t.id <trim > <if test="billsVO!=null"> <if test="billsVO.type_id!=null"> and t.id = #{billsVO.type_id} </if> <if test="billsVO.fromDate!=null"> and b.bill_time >= #{billsVO.fromDate} </if> <if test="billsVO.toDate!=null"> and b.bill_time <= #{billsVO.toDate} </if> </if> </trim> </select> <resultMap type="bills" id="listmap"> <id column="bid" property="id"/> <result column="title" property="title"/> <result column="title" property="title"/> <result column="bill_time" property="bill_time"/> <result column="price" property="price"/> <result column="explain" property="explain"/> <association property="bill_type" javaType="bill_type"> <id column="tid" property="id"/> <result column="name" property="name"/> </association> </resultMap> </mapper>
两个实体类
一个数据包装类
测试类
package com.woniu.test; import java.util.Calendar; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.woniu.dao.BillsMapper; import com.woniu.pojo.Bills; import com.woniu.vo.BillsVO; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath*:spring-*.xml") public class BillsDaoTest { @Autowired BillsMapper billsMapper; @Test public void testselectBillsList() { BillsVO billsVO = new BillsVO(); billsVO.setT_id(1); Calendar c = Calendar.getInstance(); c.set(2019, 7, 8); billsVO.setFromDate(c.getTime()); List<Bills> selectBillsList = billsMapper.selectBillsList(1, 2, billsVO); System.out.println(selectBillsList); } }
控制台测试数据
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. [DEBUG] ==> Preparing: SELECT count(0) FROM bills b, bill_type t WHERE b.type_id = t.id AND t.id = ? AND b.bill_time >= ? [main] com.woniu.dao.BillsMapper.selectBillsList_COUNT [org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:142)] [DEBUG] ==> Parameters: 1(Integer), 2019-08-08 09:02:33.468(Timestamp) [main] com.woniu.dao.BillsMapper.selectBillsList_COUNT [org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:142)] [DEBUG] <== Total: 1 [main] com.woniu.dao.BillsMapper.selectBillsList_COUNT [org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:142)] [DEBUG] ==> Preparing: select *,b.id bid,t.id tid from bills b,bill_type t where b.type_id = t.id and t.id = ? and b.bill_time >= ? LIMIT ? [main] com.woniu.dao.BillsMapper.selectBillsList [org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:142)] [DEBUG] ==> Parameters: 1(Integer), 2019-08-08 09:02:33.468(Timestamp), 2(Integer) [main] com.woniu.dao.BillsMapper.selectBillsList [org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:142)] [DEBUG] <== Total: 1 [main] com.woniu.dao.BillsMapper.selectBillsList [org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:142)] Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=1, pages=1, reasonable=false, pageSizeZero=false}[com.woniu.pojo.Bills@39a87e72]
mysql数据库两个表
CREATE TABLE `bills` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`bill_time` date NOT NULL,
`type_id` int(11) NOT NULL,
`price` float NOT NULL,
`explain` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
CREATE TABLE `bill_type` (
`id` int(11) NOT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
一开始进去的主页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="<c:url value='/js/jquery-2.2.3.min.js'/>"></script> <link rel="stylesheet" href="<c:url value='/bootstrap/css/bootstrap.min.css'/>" /> <script type="text/javascript" src="<c:url value='/js/ajaxlist.js'/>"></script> <script type="text/javascript" src="<c:url value='/js/bootstrap/js/bootstrap.min.js'/>"></script> <script type="text/javascript"> $(function(){ alert("aaa"); }); </script> </head> <body> <a href="/bkeeping/bills/list?currentPage=1">查看账单</a> </body> </html>
逆向工程使用:https://www.cnblogs.com/QQ931697811/p/5190911.html
高手的简洁配置eclipse:https://www.cnblogs.com/liangguangqiong/p/7965778.html
高手的简洁配置idea:https://www.cnblogs.com/toutou/p/ssm_spring.html