1、创建一个maven Project项目如下图:步骤 File->New->others搜索maven
2、按照上面教程,即可创建出一个Maven项目,项目结构如下图
3、将JRE版本设置为1.8,可使用默认的或者自己导入
4、配置 pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.lyy</groupId> 5 <artifactId>maver_ssm</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>maver_ssm Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <properties> 11 <srping.version>4.0.2.RELEASE</srping.version> 12 <mybatis.version>3.2.8</mybatis.version> 13 <slf4j.version>1.7.12</slf4j.version> 14 <log4j.version>1.2.17</log4j.version> 15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 16 </properties> 17 <dependencies> 18 <!-- 单元测试 --> 19 <dependency> 20 <groupId>junit</groupId> 21 <artifactId>junit</artifactId> 22 <version>3.8.1</version> 23 <scope>test</scope> 24 </dependency> 25 <dependency> 26 <groupId>javax</groupId> 27 <artifactId>javaee-api</artifactId> 28 <version>8.0</version> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-test</artifactId> 33 <version>${srping.version}</version> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-core</artifactId> 38 <version>${srping.version}</version> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework</groupId> 42 <artifactId>spring-oxm</artifactId> 43 <version>${srping.version}</version> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-tx</artifactId> 48 <version>${srping.version}</version> 49 </dependency> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-jdbc</artifactId> 53 <version>${srping.version}</version> 54 </dependency> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-aop</artifactId> 58 <version>${srping.version}</version> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework</groupId> 62 <artifactId>spring-context</artifactId> 63 <version>${srping.version}</version> 64 </dependency> 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-context-support</artifactId> 68 <version>${srping.version}</version> 69 </dependency> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-expression</artifactId> 73 <version>${srping.version}</version> 74 </dependency> 75 <dependency> 76 <groupId>org.springframework</groupId> 77 <artifactId>spring-orm</artifactId> 78 <version>${srping.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-web</artifactId> 83 <version>${srping.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-webmvc</artifactId> 88 <version>${srping.version}</version> 89 </dependency> 90 <!-- spring框架包 end --> 91 <!-- mybatis框架包 start --> 92 <dependency> 93 <groupId>org.mybatis</groupId> 94 <artifactId>mybatis</artifactId> 95 <version>${mybatis.version}</version> 96 </dependency> 97 <dependency> 98 <groupId>org.mybatis</groupId> 99 <artifactId>mybatis-spring</artifactId> 100 <version>1.2.2</version> 101 </dependency> 102 <dependency> 103 <groupId>mysql</groupId> 104 <artifactId>mysql-connector-java</artifactId> 105 <version>5.1.35</version> 106 </dependency> 107 <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> 108 <dependency> 109 <groupId>commons-dbcp</groupId> 110 <artifactId>commons-dbcp</artifactId> 111 <version>1.4</version> 112 </dependency> 113 <dependency> 114 <groupId>jstl</groupId> 115 <artifactId>jstl</artifactId> 116 <version>1.2</version> 117 </dependency> 118 <dependency> 119 <groupId>log4j</groupId> 120 <artifactId>log4j</artifactId> 121 <version>${log4j.version}</version> 122 </dependency> 123 <dependency> 124 <groupId>org.slf4j</groupId> 125 <artifactId>slf4j-api</artifactId> 126 <version>${slf4j.version}</version> 127 </dependency> 128 <dependency> 129 <groupId>org.slf4j</groupId> 130 <artifactId>slf4j-log4j12</artifactId> 131 <version>${slf4j.version}</version> 132 </dependency> 133 <dependency> 134 <groupId>com.alibaba</groupId> 135 <artifactId>fastjson</artifactId> 136 <version>1.2.6</version> 137 </dependency> 138 <dependency> 139 <groupId>org.codehaus.jackson</groupId> 140 <artifactId>jackson-mapper-asl</artifactId> 141 <version>1.9.13</version> 142 </dependency> 143 <dependency> 144 <groupId>commons-fileupload</groupId> 145 <artifactId>commons-fileupload</artifactId> 146 <version>1.3.1</version> 147 </dependency> 148 <dependency> 149 <groupId>commons-io</groupId> 150 <artifactId>commons-io</artifactId> 151 <version>2.4</version> 152 </dependency> 153 <dependency> 154 <groupId>commons-codec</groupId> 155 <artifactId>commons-codec</artifactId> 156 <version>1.10</version> 157 </dependency> 158 <!-- 上传组件包 end --> 159 </dependencies> 160 <build> 161 <finalName>maver_ssm</finalName> 162 <resources> 163 <resource> 164 <directory>src/main/java</directory> 165 <includes> 166 <include>**/*.xml</include> 167 </includes> 168 <filtering>true</filtering> 169 </resource> 170 </resources> 171 </build> 172 </project>
5、在src/main/resources下添加properties文件夹,spring文件夹
(1)在properties文件夹然后添加jdbc.properties
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/qiongliao_test 3 jdbc.username=root 4 jdbc.password=root 5 #u5B9Au4E49u521Du59CBu8FDEu63A5u6570 6 jdbc.initialSize=0 7 #u5B9Au4E49u6700u5927u8FDEu63A5u6570 8 jdbc.maxActive=20 9 #u5B9Au4E49u6700u5927u7A7Au95F2 10 jdbc.maxIdle=20 11 #u5B9Au4E49u6700u5C0Fu7A7Au95F2 12 jdbc.minIdle=1 13 #u5B9Au4E49u6700u957Fu7B49u5F85u65F6u95F4 14 jdbc.maxWait=60000
(2)在properties文件夹然后添加log4j.properties
#This is the configuring for logging displayed in the Application Server log4j.rootCategory=WARN,stdout #stdout configure log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern= %d %p [%t][%c.%M()][%L] - <%m>%n #cxf configure log4j.appender.cxf=org.apache.log4j.DailyRollingFileAppender log4j.appender.cxf.File=../logs/cxf.log log4j.appender.cxf.layout=org.apache.log4j.PatternLayout log4j.appender.cxf.layout.ConversionPattern= %d %p [%t][%c.%M()][%L] - <%m>%n #third configure log4j.appender.third=org.apache.log4j.DailyRollingFileAppender log4j.appender.third.File=../logs/third.log log4j.appender.third.layout=org.apache.log4j.PatternLayout log4j.appender.third.layout.ConversionPattern= %d %p [%t][%c.%M()][%L] - <%m>%n #fmb-platform-finance-server log4j.appender.financeServer=org.apache.log4j.DailyRollingFileAppender log4j.appender.financeServer.File=../logs/fmb-platform-finance.log log4j.appender.financeServer.layout=org.apache.log4j.PatternLayout log4j.appender.financeServer.layout.ConversionPattern= %d %p [%t][%c.%M()][%L] - <%m>%n #fmb-platform-openapi log4j.appender.openApi=org.apache.log4j.DailyRollingFileAppender log4j.appender.openApi.File=../logs/fmb-platform-openapi.log log4j.appender.openApi.layout=org.apache.log4j.PatternLayout log4j.appender.openApi.layout.ConversionPattern= %d %p [%t][%c.%M()][%L] - <%m>%n #fmb-platform-external log4j.appender.external=org.apache.log4j.DailyRollingFileAppender log4j.appender.external.File=../logs/fmb-platform-external.log log4j.appender.external.layout=org.apache.log4j.PatternLayout log4j.appender.external.layout.ConversionPattern= %d %p [%t][%c.%M()][%L] - <%m>%n # Control logging for 3rd library, changing log level to DEBUG when debug log4j.logger.org.springframework=WARN,third log4j.logger.org.hibernate=WARN,third log4j.logger.net.sf.ehcache=WARN,third log4j.logger.org.apache.cxf=WARN,cxf log4j.logger.org.apache.catalina=WARN,cxf # Changing the log level to DEBUG will display SQL Hibernate generated log4j.logger.org.hibernate.SQL=ERROR,third # fmb platform project log log4j.logger.cn.nnj.platform.external=INFO,financeServer log4j.logger.cn.nnj.openapi=INFO,openApi log4j.logger.cn.nnj.openapi=INFO,external log4j.logger.java.sql.Connection=WARN log4j.logger.java.sql.Statement=WARN log4j.logger.java.sql.PreparedStatement=WARN
(3)在spring文件夹然后添加spring-dao.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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。 可指定多个包,包与包之间用逗号或分号分隔--> <property name="basePackage" value="com.lyy.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
(4)在spring文件夹然后添加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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:properties/jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${jdbc.initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${jdbc.maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${jdbc.maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${jdbc.minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${jdbc.maxWait}"></property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/lyy/mapper/*.xml"></property> </bean> </beans>
(5)在spring文件夹然后添加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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 添加注解驱动 --> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <!-- 设置使用注解的类所在的包 --> <context:component-scan base-package="com.lyy.controller" /> <!-- 完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 --> </list> </property> </bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
(6)在spring文件夹然后添加spring-tx.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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
(7)在src/main/resources文件下添加applicationContext.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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 使用注解式注入 --> <context:annotation-config /> <!-- 自动扫描 --> <context:component-scan base-package="com.lyy" /> <!-- 导入DAO配置 --> <import resource="classpath:spring/spring-dao.xml"/> <!-- 导入数据库配置 --> <import resource="classpath:spring/spring-db.xml"/> <!-- 导入数据库配置 --> <import resource="classpath:spring/spring-tx.xml"/> </beans>
6、修改/WEB-INF/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_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 加载spring bean --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
7、在src/main/java添加如下包
8、代码开始
(1)dto层 UserDto
package com.lyy.dto; public class UserDto { private Integer userid;//用户id private String username;//用户名 public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
(2)dao层 UserDao
package com.lyy.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.lyy.dto.UserDto; public interface UserDao { List<UserDto> findByAll(@Param("pageSize")Integer pageSize,@Param("page")Integer page); }
(3)mapper层 UserMapper.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.lyy.dao.UserDao" > <resultMap id="userMap" type="com.lyy.dto.UserDto"> <result column="userid" property="userid"/> <result column="username" property="username"/> </resultMap> <!-- 查询用户--> <select id="findByAll" resultMap="userMap"> SELECT u.userid,u.username from lv_user u where u.deleted=0 and u.flag=1 <if test="pageSize!=null and page!=null"> LIMIT #{page},#{pageSize} </if> </select> </mapper>
(4)service层 UserService
package com.lyy.service; import java.util.List; import com.lyy.dto.UserDto; public interface UserService { List<UserDto> findByAll(Integer pageSize,Integer page); }
(5)业务实现层 UserServiceImpl
package com.lyy.serviceimpl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.lyy.dao.UserDao; import com.lyy.dto.UserDto; import com.lyy.service.UserService; @Service public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public List<UserDto> findByAll(Integer pageSize, Integer page) { Integer pageSizes=pageSize==null?10:pageSize; Integer pages=page==null?0:(page-1)*pageSizes; return userDao.findByAll(pageSizes, pages); } public UserDto userListDatil(Integer userid) { return userDao.userListDatil(userid); } }
(6)UserController代码 UserController
package com.lyy.controller; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.lyy.base.BasicResult; import com.lyy.dto.UserDto; import com.lyy.service.UserService; @Controller @RequestMapping("user") public class UserController { @Resource private UserService userService;
@RequestMapping("/userList") public String userList(HttpServletRequest request,Model model,Integer pageSize,Integer page){ List<UserDto> uList = userService.findByAll(pageSize, page); model.addAttribute("uList", uList); return "userList"; } }
8、在WEB-INF添加一个jsp文件夹
jsp文件夹下添加 userList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>用户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <table> <tr> <td> 用户名称</td> <td> 用户id</td> </tr> <c:forEach items="${uList}" var="u"> <tr> <td> ${u.username} <td> <td> ${u.userid} </td> </tr> </c:forEach> </table> </body> </html>