一、创建项目,整合SSM(spring mvc + spring + mybatis)
------------------------------------------------------------
1.创建模块,添加web支持,添加maven支持
2.添加maven依赖
mysql驱动
c3p0数据源
mybatis
spring(tx | aop | context))
mybatis-spring
spring mvc
-------------------------------------------------
<?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.it18zhang</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
3.创建包
test.ssm.domain
test.ssm.dao
test.ssm.service
test.ssm.util
test.ssm.web.controller
4.创建基本类库
[test.ssm.domain]
test.ssm.domain.Items
test.ssm.domain.Orders
test.ssm.domain.Users
[test.ssm.dao]
test.ssm.dao.IBaseDao
[test.ssm.dao.impl]
test.ssm.dao.UserDao
test.ssm.dao.OrderDao
[test.ssm.service]
test.ssm.service.IBaseService
test.ssm.service.IOrderService
test.ssm.service.IUserService
[test.ssm.service.impl]
test.ssm.service.BaseServiceImpl
test.ssm.service.OrderServiceImpl
test.ssm.service.UserServiceImpl
5.编写beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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" default-autowire="byType">
<!-- 配置扫描组件 -->
<context:component-scan base-package="test.ssm.dao,test.ssm.service"/>
<!-- 配置c3p0池化数据源bean -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?user=mysql&password=mysql"/>
<property name="user" value="mysql"/>
<property name="password" value="mysql"/>
<property name="maxPoolSize" value="10"/>
<property name="minPoolSize" value="2"/>
<property name="initialPoolSize" value="3"/>
<property name="acquireIncrement" value="2"/>
</bean>
<!-- 配置会话工厂bean -->
<!-- mybatis 整合 spring 的核心类 -->
<bean id="sf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 给数据源dataSource属性指定关联类,类id为dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- 给属性configLocation属性赋值,值为mybatis-congif.xml -->
<property name="configLocation" value="mybatis-config.xml"/>
</bean>
<!-- 配置事务的属性:开启何种事务 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- * 表示对所有的方法使用默认的隔离级别 -->
<!-- 也可以 tx:method name="insert *" 对所有的插入函数使用默认的隔离级别 -->
<!-- propagation 传播行为-->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务切面:在何处开启事务,pointcut切入点 -->
<!-- execution(* *..*Service.*(..)) -->
<!-- execution(返回值 包.*Service.*(..)) -->
<!-- 任何返回值的 任何包下的以Service结尾的类中的任何方法(..表示参数不限) :其实就是表示一类方法-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))" />
</aop:config>
<!-- 配置数据源事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
6.编写mybatis-config.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 type="test.ssm.domain.Users" alias="_Users"/>
<typeAlias type="test.ssm.domain.Orders" alias="_Orders"/>
<typeAlias type="test.ssm.domain.Items" alias="_Items"/>
</typeAliases>
<mappers>
<mapper resource="UserMapper.xml"/>
<mapper resource="OrderMapper.xml"/>
</mappers>
</configuration>
7.编写映射文件OederMapper.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="orders">
<insert id="insert">
insert into orders (orderno,uid) values (#{orderno},#{user.id});
</insert>
<select id="select_one" parameterType="int" resultType="_Orders">
select * from orders where id = #{id};
</select>
<!-- 查询多个 注意查询要指定结果的类型-->
<select id="select_all" resultType="_Orders">
select * from orders;
</select>
<select id="select_by_id" parameterType="int" resultType="_Orders">
select * from orders where id = #{id};
</select>
<!-- 关联查询:左外连接查询 -->
<select id="select_join_one" parameterType="int" resultMap="MyResMap">
select
o.id oid ,
o.orderno ono,
o.uid uid,
u.name uname,
u.age uage
from orders o
left outer join users u
on o.uid=u.id
where o.id= #{id};
</select>
<!-- 关联查询:左外连接查询 -->
<select id="select_join_all" parameterType="int" resultMap="MyResMap">
select
o.id oid ,
o.orderno ono,
o.uid uid,
u.name uname,
u.age uage
from orders o
left outer join users u
on o.uid=u.id;
</select>
<!-- 三级关联查询:用户-订单-items -->
<select id="select_join_3_one" parameterType="int" resultMap="MyResMap">
select
o.id oid ,
o.orderno ono,
o.uid uid,
u.name uname,
u.age uage,
i.id iid,
i.itemname iitemname
from orders o
left outer join users u on o.uid=u.id
left outer join items i on o.id = i.oid
where o.id = #{id};
</select>
<!-- 三级关联查询:用户-订单-items -->
<select id="select_join_3_all" resultMap="MyResMap">
select
o.id oid ,
o.orderno ono,
o.uid uid,
u.name uname,
u.age uage,
i.id iid,
i.itemname iitemname
from orders o
left outer join users u on o.uid=u.id
left outer join items i on o.id = i.oid;
</select>
<!-- 指定:关联查询别名的映射 将类的属性与表中列的别名进行映射-->
<resultMap id="MyResMap" type="_Orders">
<!-- 主键用id ,其余用 result -->
<id property="id" column="oid"/>
<result property="orderno" column="ono"/>
<!-- 关联的类要特殊处理,如下 -->
<association property="user" javaType="_Users">
<id property="id" column="uid"/>
<result property="name" column="uname"/>
<result property="age" column="uage"/>
</association>
<!-- 处理集合属性 -->
<collection property="itemList" ofType="_Items">
<id property="id" column="iid"/>
<result property="itemname" column="iitemname"/>
</collection>
</resultMap>
</mapper>
8.编写映射文件OederMapper.xml/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="users">
<insert id="insert">
<!--#{name},#{age} 表示参数,占位符-->
insert into users (name,age) values (#{name},#{age});
</insert>
<update id="update">
update users set name = #{name} ,age = #{age} where id = #{id};
</update>
<delete id="delete">
delete from users where id = #{id};
</delete>
<!-- 查询一个 注意查询要指定结果的类型-->
<select id="select_one" parameterType="int" resultType="_Users">
select * from users where id = #{id};
</select>
<!-- 查询多个 注意查询要指定结果的类型-->
<select id="select_all" resultType="_Users">
select * from users;
</select>
<select id="select_join_one" parameterType="int" resultMap="userMap">
select
u.id uid,
u.name uname,
u.age uage,
o.id oid,
o.orderno ono
from users u
left outer join orders o
on u.id = o.uid
where u.id = #{id};
</select>
<select id="select_join_all" resultMap="userMap">
select
u.id uid,
u.name uname,
u.age uage,
o.id oid,
o.orderno ono
from users u
left outer join orders o
on u.id = o.uid;
</select>
<!-- 别名映射 将类中的属性名与表的列明或者列的别名进行映射 -->
<resultMap id="userMap" type="_Users">
<id property="id" column="uid"/>
<result property="name" column="uname"/>
<result property="age" column="uage"/>
<!-- Users中的属性 List<Orders> list 的表示方式 -->
<!-- property 表示属性名称 foType表示集合中的元素的类型 -->
<collection property="list" ofType="_Orders">
<id property="id" column="oid"/>
<result property="orderno" column="ono"/>
</collection>
</resultMap>
</mapper>
9.编写spring mvc的UserController
------------------------------------------------
package test.ssm.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import test.ssm.domain.Users;
import test.ssm.service.impl.UserServiceImpl;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class UserController {
//注入UserService
@Resource(name="userService")
private UserServiceImpl us;
/**
* 查看全部User
*/
@RequestMapping({"/user/findAllUser"})
public String findAllUser(Model model, HttpSession session)
{
List<Users> list = us.selectAll();
model.addAttribute("allUsers", list);
return "user/userList";
}
}
10.编写/user/userList.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<table border="1px">
<tr>
<td>ID</td>
<td>NAME</td>
<td>AGE</td>
<td>删除</td>
<td>修改</td>
</tr>
<c:forEach items="${allUsers}" var="u">
<tr>
<td><c:out value="${u.id}"/>
<td><c:out value="${u.name}"/>
<td><c:out value="${u.age}"/>
<td><a href="<c:url value="/user/deleteUser?uid=${u.id}"/>">删除</a></td>
<td><a href="<c:url value="/user/editUser?uid=${u.id}"/>">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
11.编写WEB-INF/web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 指定spring的配置文件beans.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans.xml</param-value>
</context-param>
<!-- 设置监听,在web启动的时候,加载beans.xml和分发器dispatcher-servlet.xml等配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置分发器Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
12.编写WEB-INF/dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.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.spring.controller" />
<!-- 使用注解驱动 -->
<mvc:annotation-driven />
<!-- 内部资源视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix + controller return + suffix-->
<!-- 前缀 + 控制器返回值 + 后缀 -->
<!-- / index .jsp -->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
13.运行配置
1.项目配置 File --> Project Structure --> Artifacts --> TestSSM --> Put Into web Lib --> 将右侧所有的包全部添加到web/lib下
2.运行配置 Run --> Edit Configurations --> + --> tomcat --> Deployment --> + --> TestSSM
3.热部署 Run --> Edit Configurations --> Server --> On Update Action / On Frame ... --> Update class and Resources
14.运行程序,打开网址http://localhost:8080/user/findall
二、访问spring mvc的静态资源
------------------------------------------------
1.描述--这些静态资源没必要像jsp页面一样进行计算
html
image
css
js
2.在[dispatcher-servlet.xml]添加如下元素,将文件类型和路径进行映射关联[如果不关联,那么会找不到静态资源404]
<mvc:resources mapping="/jsssss/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/html/**" location="/html/"/>
3.创建关联目录
/web/js
/web/css
/web/images
/web/html
4.放置相应文件
/web/html/hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello.html</title>
</head>
<body>
<div>hello</div>
<table>
<tr>
<td>ID</td>
<td>name</td>
<td>age</td>
</tr>
<tr>
<td>1</td>
<td>tom</td>
<td>12</td>
</tr>
<tr>
<td>2</td>
<td>tomas</td>
<td>13</td>
</tr>
<tr>
<td>3</td>
<td>tomasLee</td>
<td>4</td>
</tr>
</table>
</body>
</html>
5.启动程序访问
http://localhost:9090/html/hello.html
三、处理中文乱码
------------------------------------------
1.jsp页面使用<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>进行utf8码声明。
2.在web.xml文件中,加入filter。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 指定spring的配置文件beans.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:beans.xml</param-value>
</context-param>
<!-- 确保web服务器启动时,完成spring的容器初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置分发器Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.确认mysql使用utf8编码,可以修改mysql的编码.
[mysql安装目录/my.ini]
uft8
4.spring中的数据库连接地址url中显式指定编码.
[beans.xml]
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?user=root&password=root&useUnicode=true&characterEncoding=utf8"/>