-----------------------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; } publicvoid setUid(Integer uid) { this.uid = uid; }
public String getUsername() { return username; } publicvoid setUsername(String username) { this.username = username; }
public String getPassword() { return password; } publicvoid setPassword(String password) { this.password = password; }
public String getAddress() { return address; } publicvoid 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;
publicvoid setUserService(UserService userService) { this.userService = userService; }
/** * 注意:这里没有返回值,仅仅返回了一个 HTTP 状态码 200 */ @RequestMapping("/show") @ResponseStatus(HttpStatus.OK) publicvoid 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;
publicvoid 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:
version="1.0"encoding="UTF-8"?> mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from t_user where uid = #{uid}
|
(6)在MyBatis 核心配置文件中进行配置
mybatis-config.xml:
version="1.0"encoding="UTF-8"?> configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
(7)在数据库连接信息的属性文件中进行配置
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test_db
jdbc.username=root
jdbc.password=8888 |
其中:
jdbc:mysql:///test_db 是 jdbc:mysql://localhost:3306/test_db 的简写
(8)在Spring 核心配置文件中进行配置
applicationContext.xml:
version="1.0"encoding="UTF-8"?> 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">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
(9)在SpringMVC 核心配置文件中进行配置
dispatcher-servlet.xml:
version="1.0"encoding="UTF-8"?> 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">
|
(10)在部署描述文件中进行配置
web.xml:
version="1.0"encoding="UTF-8"?>
|
(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)加上
(4) 在 SpringMVC 核心配置文件中:
1)删掉 userController 的 Bean
2)加上
或综合(3)(4):删掉两个Bean,在 SpringMVC 核心配置文件中
加上如下内容:
【made by siwuxie095】