-----------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

SSM 框架整合过程总结

 
 

 
 

1、导入相关
jar 包(共 26 个)

 
 

(1)导入
Spring 的核心 jar 包和日志相关的 jar 包(6 个)

 
 

 
 

 
 

Commons Logging
下载链接:

 
 

http://commons.apache.org/proper/commons-logging/download_logging.cgi

 
 

 
 

LOG4J 下载链接:

 
 

https://www.apache.org/dist/logging/log4j/

 
 

 
 

 
 

(2)导入
Spring 的 AOP 开发的 jar 包(4 个)

 
 

 
 

 
 

AOP Alliance
下载链接:

 
 

http://mvnrepository.com/artifact/aopalliance/aopalliance

 
 

 
 

AspectJ Weaver
下载链接:

 
 

http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

 
 

 
 

 
 

(3)导入
Spring 的
JDBC 开发的 jar 包(2 个)

 
 

 
 

 
 

 
 

(4)导入
Spring 整合 Web 项目的 jar 包(1 个)

 
 

 
 

 
 

(5)导入
SpringMVC 的 jar 包(1 个)

 
 

 
 

 
 

 
 

(6)导入
SpringMVC 中使用 JSON 所需的 jar 包(3 个)

 
 

 
 

 
 

Jackson Core 下载链接:

 
 

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/

 
 

 
 

Jackson Annotations 下载链接:

 
 

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/

 
 

 
 

Jackson Databind 下载链接:

 
 

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/

 
 

 
 

 
 

(7)导入
MyBatis 的 jar 包(1 个)

 
 

 
 

 
 

 
 

(8)导入
MyBatis 所需的日志相关的 jar 包(2 个)

 
 

 
 

「MyBatis 也需要 LOG4J,但前面 Spring 已导入,不再重复导入」

 
 

 
 

 
 

(9)导入
MyBatis 分页相关的 jar 包(2 个)

 
 

 
 

 
 

PageHelper 下载链接:

 
 

http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/

 
 

 
 

JSqlParser 下载链接:

 
 

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.5/

 
 

 
 

(10)导入
MySQL 的 JDBC 驱动的 jar 包(1 个)

 
 

 
 

 
 

MySQL Connector/J
下载链接:

 
 

https://dev.mysql.com/downloads/connector/j/

 
 

 
 

 
 

(11)导入
Spring 整合 MyBatis 的 jar 包(1 个)

 
 

 
 

 
 

MyBatis-Spring 下载链接:

 
 

https://github.com/mybatis/spring/releases

 
 

 
 

 
 

(12)导入
BoneCP 的 jar 包和其依赖的 Guava 的 jar 包(2 个)

 
 

 
 

 
 

BoneCP 下载链接:

 
 

http://repo1.maven.org/maven2/com/jolbox/bonecp/

 
 

 
 

Guava 下载链接:

 
 

http://repo1.maven.org/maven2/com/google/guava/guava/

 
 

 
 

 
 

 
 

 
 

2、创建数据库和表

 
 

创建数据库
test_db, 再创建表 t_user,并插入若干数据,

其中:uid 为主键,且为自动增长

 
 

 
 

 
 

 
 

 
 

 
 

3、测试

 
 

(1)编写一个实体类

 
 

User.java:

 
 

package com.siwuxie095.entity;

 
 

// 实体类

public class User {

 

private Integer uid;

private String username;

private String password;

private String address;

 

public Integer getUid() {

return uid;

}

public
void setUid(Integer uid) {

this.uid = uid;

}

 

public String getUsername() {

return username;

}

public
void setUsername(String username) {

this.username = username;

}

 

public String getPassword() {

return password;

}

public
void setPassword(String password) {

this.password = password;

}

 

public String getAddress() {

return address;

}

public
void setAddress(String address) {

this.address = address;

}

 

@Override

public String toString() {

return
"User [uid=" + uid + ", username=" + username +

", password=" + password + ", address=" + address + "]";

}

 

}

 
 

 
 

 
 

(2)编写一个
Controller 类

 
 

UserController.java:

 
 

package com.siwuxie095.controller;

 
 

import org.springframework.http.HttpStatus;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseStatus;

 
 

import com.siwuxie095.entity.User;

import com.siwuxie095.service.UserService;

 
 

// Controller 类

@Controller

@RequestMapping("/user")

public class UserController {

 

private UserService userService;

 

public
void setUserService(UserService userService) {

this.userService = userService;

}

 

/**

*/

@RequestMapping("/show")

@ResponseStatus(HttpStatus.OK)

public
void show() {

 

User user = userService.getUser(1);

System.out.println(user);

}

 

}

 
 

 
 

 
 

(3)编写一个
Service 类

 
 

UserService.java:

 
 

package com.siwuxie095.service;

 
 

import org.springframework.transaction.annotation.Transactional;

 
 

import com.siwuxie095.entity.User;

import com.siwuxie095.mapper.UserMapper;

 
 

// Service 类

@Transactional

public class UserService {

 
 

private UserMapper userMapper;

 

public
void setUserMapper(UserMapper userMapper) {

this.userMapper = userMapper;

}

 

public User getUser(Integer uid){

return userMapper.getUser(uid);

}

 

}

 
 

 
 

 
 

(4)编写一个映射器接口

 
 

UserMapper.java:

 
 

package com.siwuxie095.mapper;

 
 

import org.apache.ibatis.annotations.Param;

 
 

import com.siwuxie095.entity.User;

 
 

// 映射器接口

public interface UserMapper {

 
 

User getUser(@Param("uid") Integer uid);

 

}

 
 

 
 

 
 

(5)在
MyBatis 映射配置文件中进行配置

 
 

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.siwuxie095.mapper.UserMapper">

 
 

<select
id="getUser"
resultType="User">

select * from t_user where uid = #{uid}

</select>

 

</mapper>

 
 

 
 

 
 

(6)在
MyBatis 核心配置文件中进行配置

 
 

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>

 

<settings>

<!-- 开启自动驼峰命名规则映射 -->

<setting
name="mapUnderscoreToCamelCase"
value="true"/>

</settings>

 

<!-- 配置类型别名 -->

<typeAliases>

<typeAlias
type="com.siwuxie095.entity.User"
alias="User"/>

</typeAliases>

 

<!-- 引入映射配置文件 -->

<mappers>

<package
name="com.siwuxie095.mapper"/>

</mappers>

 
 

</configuration>

 
 

 
 

 
 

(7)在数据库连接信息的属性文件中进行配置

 
 

jdbc.properties:

 
 

jdbc.driverClassName=com.mysql.jdbc.Driver

 
 

jdbc.url=jdbc:mysql:///test_db

 
 

jdbc.username=root

 
 

 
 

其中:

 
 

jdbc:mysql:///test_db 是 jdbc:mysql://localhost:3306/test_db 的简写

 
 

 
 

 
 

(8)在
Spring 核心配置文件中进行配置

 
 

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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.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">

 

 

<!-- 使用spring自带的占位符替换功能 -->

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

 

<!-- 允许JVM参数覆盖 -->

<property
name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>

 

<!-- 忽略没有找到的资源文件 -->

<property
name="ignoreResourceNotFound"
value="true"/>

 

<!-- 配置资源文件(也称
外部属性文件) -->

<property
name="locations">

<list>

<value>classpath:jdbc.properties</value>

</list>

</property>

 

</bean>

 

 

<!-- 配置 BoneCP 连接池 -->

<bean
id="dataSource"
class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">

 

<!-- 数据库驱动 -->

<property
name="driverClass"
value="${jdbc.driverClassName}"
/>

 

<!-- 相应驱动的jdbcUrl -->

<property
name="jdbcUrl"
value="${jdbc.url}"
/>

 

<!-- 数据库的用户名 -->

<property
name="username"
value="${jdbc.username}"
/>

 

<!-- 数据库的密码 -->

<property
name="password"
value="${jdbc.password}"
/>

 

,如果要取消则设置为0 -->

<property
name="idleConnectionTestPeriod"
value="60"
/>

 

,如果要永远存活设置为0 -->

<property
name="idleMaxAge"
value="30"
/>

 

<!-- 每个分区最大的连接数 -->

<property
name="maxConnectionsPerPartition"
value="150"
/>

 

<!-- 每个分区最小的连接数 -->

<property
name="minConnectionsPerPartition"
value="5"
/>

 

</bean>

 

 

<!-- 将 SqlSessionFactory 对象的创建交给 Spring 进行管理 -->

<bean
id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 指定数据源 -->

<property
name="dataSource"
ref="dataSource"
/>

<!-- 指定 MyBatis 核心配置文件的位置(路径) -->

<property
name="configLocation"
value="classpath:mybatis-config.xml"></property>

</bean>

 

 

<!-- 配置 Service 对象 -->

<bean
id="userService"
class="com.siwuxie095.service.UserService">

<property
name="userMapper"
ref="userMapper"></property>

</bean>

 

 

<!-- 配置映射器接口(Mapper 接口)的扫描包 -->

<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 如有多个包,以逗号

分号隔开即可 -->

<property
name="basePackage"
value="com.siwuxie095.mapper"/>

</bean>

 

 

<!--

配置映射器接口(以下方法二选一即可,这里选择法二):

 

法一:逐个配置:配置映射器接口(Mapper 接口)的对象

 

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

<property name="mapperInterface" value="com.siwuxie095.mapper.UserMapper"/>

<property name="sqlSessionFactory" ref="sqlSessionFactory"/>

</bean>

 

 

法二:统一配置:配置映射器接口(Mapper 接口)的扫描包

 

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.siwuxie095.mapper"/>

</bean>

 

-->

 

 

<!-- 配置事务管理器 -->

<bean
id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!-- 注入 dataSource -->

<property
name="dataSource"
ref="dataSource"
/>

</bean>

 
 

<!-- 配置事务注解,即
开启事务注解 -->

<tx:annotation-driven
transaction-manager="transactionManager"/>

 

<!-- 一般事务管理都是在 Service 层进行,只需在 Service 类上加上 @Transactional 注解 -->

 
 

 
 

</beans>

 
 

 
 

 
 

(9)在
SpringMVC 核心配置文件中进行配置

 
 

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: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">

 

 

<!-- 启用注解驱动 -->

<mvc:annotation-driven/>

 

 

<!--

配置 Controller(必须,即
必须进行配置)

 

class 为自定义 Controller 类的完全限定名,这里通过 Controller

类中的 @RequestMapping 注解来设置访问路径

 

使用纯注解时,另一种配置 Controller 的方式:配置扫描包

<context:component-scan base-package="com.siwuxie095.controller" />

-->

<bean
id="userController"
class="com.siwuxie095.controller.UserController">

<property
name="userService"
ref="userService"/>

</bean>

 

 

<!-- 配置 ViewResolver(必须,即
必须进行配置) -->

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--

配置视图解析的前缀 prefix 和后缀 suffix:

(1)前缀:如果在 WebContent 目录下,则为 /,如果在 WEB-INF 目录下,则为 /WEB-INF/

(2)后缀:一般为 JSP 文件,所以为 .jsp

 

例如:prefix="/",suffix=".jsp",viewname="test",则:"/test.jsp"

-->

<property
name="prefix"
value="/"/>

<property
name="suffix"
value=".jsp"/>

</bean>

 

 

</beans>

 
 

 
 

 
 

(10)在部署描述文件中进行配置

 
 

web.xml:

 
 

<?xml
version="1.0"
encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>TestSpringMVCAndSpring</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>

 

 

<!-- 配置 Spring 的监听器 ContextLoaderListener -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

<!-- 配置 Spring 核心配置文件的位置(路径) -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

 

 

<!-- 配置 SpringMVC 的核心分发器 -->

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 配置 SpringMVC 核心配置文件的位置(路径) -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:dispatcher-servlet.xml</param-value>

</init-param>

<!-- 自动加载:随 Tomcat 容器启动,完成初始化 -->

<load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

 

</web-app>

 
 

 
 

 
 

(11)访问路径

 
 

http://localhost:8080/工程名/user/show.do

 
 

 
 

 
 

 
 

 
 

附:

 
 

另一种配置方式,即
纯注解,对以上「测试」做如下修改:

 
 

(1)在
Controller 类中:

 
 

1)在 userService 属性上加 @Autowired 注解

 
 

2)删掉
userService 属性的 setter 方法

 
 

 
 

(2)在
Service 类中:

 
 

1)在类上加 @Service 注解

 
 

2)在 userMapper 属性上加 @Autowired 注解

 
 

3)删掉
userMapper 属性的 setter 方法

 
 

 
 

 
 

(3)在
Spring 核心配置文件中:

 
 

1)删掉 userService 的 Bean

 
 

2)加上
<context:component-scan base-package="com.siwuxie095.service"/>

 
 

 
 

(4) 在 SpringMVC 核心配置文件中:

 
 

1)删掉 userController 的 Bean

 
 

2)加上
<context:component-scan base-package="com.siwuxie095.controller"/>

 
 

 
 


综合(3)(4):删掉两个
Bean,在 SpringMVC 核心配置文件中

加上如下内容:

 
 

<context:component-scan base-package="com.siwuxie095"/>

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

SSM框架整合过程总结的更多相关文章

  1. ssm框架整合-过程总结(第二次周总结)

    距离上次写博客已经有4.5天的时间了. 这次写博客目的是总结一下项目开始到现在,过程中遇到的问题.和学到的知识.经验. 初略总结下自己从中学到的: Spring :在学习中被反复强调的Ioc(反转控制 ...

  2. ssm框架整合-过程总结(第三次周总结)

    本周主要是完成前端界面和后端的整合. 犹豫前后端的工作完成程度不一致,只实现了部分整合. 登录界面. 可能自己最近没有把重心放在短学期的项目上,导致我们工作的总体进度都要比别慢. 虽然我们只是三个人的 ...

  3. 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  4. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  5. SSM框架整合:转自:http://blog.csdn.net/zhshulin

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  6. (转)淘淘商城系列——SSM框架整合之Dao层整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...

  7. 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  8. SSM框架整合项目 :租房管理系统

    使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...

  9. 基于maven的ssm框架整合

    基于maven的ssm框架整合 第一步:通过maven建立一个web项目.                第二步:pom文件导入jar包                              (1 ...

随机推荐

  1. JavaScript 实现打印操作

    一.打印当前页面指定元素中的内容 方式一:直接使用window.print(); (1)首先获得元素的html内容(这里建议如果有样式最好是用内联样式的方式) var newstr = documen ...

  2. C++11如何减少内存拷贝次数

    C++11中出现了很多迷人的特性.例如智能指针实现高效的内存管理,std::bind和std::function函数封装器,以及lambda实现的函数对象语法糖,都是使我着迷的地方. 而C++11最大 ...

  3. 新系统centos7重启网络报错

    场景: 在不知名云上新弄的centos7,改了IP之后启动不起来,使用systemctl status network查看结果如下:       排查过程:   1)NetworkManager是否关 ...

  4. Python脚本模拟僵尸进程与孤儿进程

    最近一台机器的systemd内存高达30%多,一直不变,后来排查是僵尸进程,什么是僵尸进程呢,只能google,百度等先了解,然后自己总结了一下,虽然这是基础的东西,但是对于我来说就如新大陆一样.花了 ...

  5. ubuntu命令行打开网页

    在Ubuntu下,当需要打开其他格式文件时,比如pdf.jpg.mp3等格式文件,通常做法是进入到文件所在的目录,双击打开,很影响效率.事实上,可以通过命令xdg-open打开这些格式文件,甚至是网页 ...

  6. 报错:org.apache.hadoop.hive.metastore.HiveMetaException: Failed to get schema version.

    报错环境: CDH中集成的hive服务,启动报错,所以初始化一下元数据. 配置文件:/etc/hive/conf hive-site.xml 命令目录:/opt/cloudera/parcels/CD ...

  7. 铁板纹理 Base Shape

    软件:Substance Designer 2017.1.2 最近正在根据官方的教程,学习Metal Rust纹理的制作.这篇文章仅记录Base Shape的制作方法. Base Shape最终渲染效 ...

  8. Linux背背背(2)

    目录: 1.简单命令 2.目录切换命令 3.扩展命令 简单命令 ls 语法1:#ls [路径]            表示列出指定路径下的文件夹和文件的名字,如果路径没有指定则列出当前路径下的 语法2 ...

  9. JS类型转换(强制和自动的规则)

    显式转换 通过手动进行类型转换,Javascript提供了以下转型函数: 转换为数值类型:Number(mix).parseInt(string,radix).parseFloat(string) 转 ...

  10. Struts vs spring mvc

    1. 机制.spring mvc 的入口是servlet, 而struts是filter(这里要指出,filter和servlet是不同的.以前认为filter是servlet的一种特殊),这样就导致 ...