SSM项目整合
一、maven工程
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.zengqingfa</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--spring核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--spring aop支持包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!--spring 事务包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--springmvc包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--mybatis包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--spring整合mybatis支持包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--数据库驱动包、连接池-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--servlet支持包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<!--junit测试包、spring对junit支持包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--添加jstl依赖-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--json支持-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- tomcat 插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>9005</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、domain
1、Account.java
package com.zengqingfa.entity;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:22
*
*/
/**
* PO(Persistence Object)持久化类
* 7个规范
* 1. 公有类
* 2. 公有无参构造
* 3. 私有属性
* 4. 公有的getter与setter
* 5. 实现java.io.Serializable接口
* 6. 不能用final修饰
* 7. 如果是基础类型,要使用它的包装类
*/
import java.io.Serializable;
public class Account implements Serializable {
private Integer accountid;
private Integer uid;
private Double money;
public Integer getAccountId() {
return accountid;
}
public void setAccountId(Integer accountId) {
this.accountid = accountid;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
三、controller
1、AccountController.java
package com.zengqingfa.controller;
import com.zengqingfa.entity.Account;
import com.zengqingfa.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
@Controller
public class AccountController {
// 注入service
@Autowired
private IAccountService accountService;
// 查询全部
@RequestMapping("/findAll")
public ModelAndView findAll() {
//1.调用service查询
List<Account> list = accountService.findAll();
//2.返回结果
ModelAndView mv = new ModelAndView();
mv.setViewName("list");
mv.addObject("list", list);
mv.addObject("date", new Date());
return mv;
}
@RequestMapping("/findAll2")
@ResponseBody
public List<Account> findAll2() {
//1.调用service查询
return accountService.findAll();
}
}
四、service
1、service接口
package com.zengqingfa.service;
import com.zengqingfa.entity.Account;
import java.util.List;
public interface IAccountService {
/**
* 查询全部
*/
List<Account> findAll();
}
2、service实现
package com.zengqingfa.service.impl;
import com.zengqingfa.dao.IAccountDao;
import com.zengqingfa.entity.Account;
import com.zengqingfa.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
// 创建service对象
@Service
public class AccountServiceImpl implements IAccountService {
// 注入dao
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
}
五、dao
1、dao接口
package com.zengqingfa.dao;
import com.zengqingfa.entity.Account;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface IAccountDao {
/**
* 查询全部
*/
@Select("select * from account")
List<Account> findAll();
}
六、配置文件
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"
version="2.5">
<!--加载Spring的容器的核心配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置要加载的容器的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--SpringMVC前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、springMVC.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.注解扫描-->
<context:component-scan base-package="com.zengqingfa.controller" />
<!--2.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--3.注解驱动-->
<mvc:annotation-driven />
</beans>
3、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:comtext="http://www.springframework.org/schema/context"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--1.开启注解扫描,只扫描service-->
<comtext:component-scan base-package="com.zengqingfa.service" />
<!--2. 加载配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--3. 创建连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--4.Spring整合Mybatis,把SqlSessionFactory对象的创建交给Spring管理-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--5. 创建包扫描器,扫描dao接口。自动对dao接口生成代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zengqingfa.dao"></property>
</bean>
<!--6. Spring声明式事务管理-->
<!--6.1 事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--6.2 事务通知规则(切入点表达式拦截到方法后如何应用事务)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--6.3 Aop配置-->
<aop:config>
<!--6.3.1 配置切入点表达式-->
<aop:pointcut id="pt" expression="execution(* com..*ServiceImpl.*(..))"></aop:pointcut>
<!--6.3.2 建立切入点表达式与通知规则的关系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>
4、jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/vue?characterEncoding=utf8
jdbc.username=root
jdbc.password=root
jdbc.initialSize=3
jdbc.maxActive=10
5、log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
七、页面
list.jsp
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--1.核心标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--2.格式化与国际化标签库--%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--3.函数库--%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>list</title>
</head>
<body>
<h3>foreach遍历标签:遍历数字</h3>
<c:forEach var="num" begin="1" end="10" step="2">
${num},
</c:forEach>
<h3>foreach遍历request域数据</h3>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<td>序号</td>
<td>编号</td>
<td>用户id</td>
<td>金额</td>
</tr>
<c:forEach var="account" items="${list}" varStatus="vs">
<tr>
<td>${vs.count}</td>
<td>${account.accountId}</td>
<td>${account.uid}</td>
<td>${account.money}</td>
</tr>
</c:forEach>
</table>
<h3>格式化金额、日期</h3>
<%--pattern="0.00 保留2位小数,不够两位自动补0--%>
<fmt:formatNumber value="1" pattern="0.00"></fmt:formatNumber> ,
<fmt:formatNumber value="1.985" pattern="0.00"></fmt:formatNumber> ,
<%--pattern="#.## 保留2位小数,不够两位不会自动补0--%>
<fmt:formatNumber value="1" pattern="#.##"></fmt:formatNumber> ,
<fmt:formatNumber value="1.985" pattern="#.##"></fmt:formatNumber> ,
<%--格式化日期:value 就是日期值(java.util.Date)--%>
<fmt:formatDate value="${date}" pattern="yyyy/MM/dd"></fmt:formatDate>
<h3>函数库(方便页面操作字符串函数)</h3>
${fn:contains("itcast", "cast")}
</body>
</html>
八、展示
1、http://localhost:8080/findAll.do
2、http://localhost:8080/findAll2.do
[{"uid":45,"money":1000.0,"accountId":1},{"uid":46,"money":1000.0,"accountId":2},{"uid":46,"money":2000.0,"accountId":3}]