一、SpringMVC 的入门案例
1.1 创建 Maven 工程
1. 创建 Maven 项目;
2. 添加依赖;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.17.RELEASE</version>
</dependency>
3. 添加 tomcat 插件;
<plugins>
<!-- Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8090</port> <!-- 端口号 -->
<path>/</path> <!-- /表示访问路径 省略项目名 -->
<uriEncoding>utf-8</uriEncoding> <!-- 设置编码格式 -->
</configuration>
</plugin>
</plugins>
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.gupaoedu</groupId>
<artifactId>springmvc_demo_01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc_demo_01 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.17.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc_demo_01</finalName>
<plugins>
<!-- Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8090</port> <!-- 端口号 -->
<path>/</path> <!-- /表示访问路径 省略项目名 -->
<uriEncoding>utf-8</uriEncoding> <!-- 设置编码格式 -->
</configuration>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4. 【view】-->【Tool Windows】-->【Maven】-->【Plugins】-->【tomcat7】 -->【run】测试运行;
5. 目录结构
6. 运行结果:
1.2 完善项目
1. 【src/main】下新建【java】目录,右击【Java】-->【Mark Directory as】 -->【Sources Root】;
2.【src/main】下新建【resources】目录,右击【resources】-->【Mark Directory as】-->【Resources Root】;
3. 右击【resources】-->【New】-->【XML Configuration File】-->【Spring Config】,【spring-mvc.xml】;
4.【src/main/webapp/WEB-INF/web.xml】,注册前端控制器(DispatcherServlet)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 注册前端控制器 -->
<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-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. 【src/main/resources/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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
</beans>
6. 新建【com.gupaoedu.controller.UserController.java】;
package com.gupaoedu.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
return null;
}
}
7. 添加【servlet】依赖;
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
二、基于 SpringMVC 注解的使用
1. 修改【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: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">
<!-- 开启扫描 -->
<context:component-scan base-package="com.gupaoedu.controller" />
<!-- 开启SpringMVC注解的使用 -->
<mvc:annotation-driven />
</beans>
2. 修改【UserController.java】;
package com.gupaoedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/query")
public String query() {
System.out.println("query ... ... ");
return "/index.jsp";
}
@RequestMapping("/save")
public String save() {
System.out.println("save ... ... ");
return "/index.jsp";
}
@RequestMapping("/delete")
public String delete() {
System.out.println("delete ... ... ");
return "/index.jsp";
}
@RequestMapping("/update")
public String update() {
System.out.println("update ... ... ");
return "/index.jsp";
}
}
2.1 SpringMVC 中的响应请求
2.1.1 响应字符串
@RequestMapping("/query")
public String query() {
System.out.println("query ... ... ");
return "/index.jsp";
}
2.1.2 不响应
@RequestMapping("/query")
@ResponseBody
public void query() {
System.out.println("query ... ... ");
}
2.1.3 返回 ModelAndView 对象
@RequestMapping("/query")
public ModelAndView query() {
ModelAndView mv = new ModelAndView();
System.out.println("query ... ... ");
mv.setViewName("/index.jsp");
return mv;
}
2.1.4 重定向跳转
@RequestMapping("/jump")
public String jump(){
System.out.println("jump ..... ");
return "redirect:/user/query";
}
2.1.5 视图解析器添加前后缀
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/" />
</bean>
@RequestMapping("/query")
public String query(){
System.out.println("query ..... ");
return "index";
}
2.1.6 通过 HttpServletResponse 响应
@RequestMapping("/fun1")
public void fun1(HttpServletRequest request
,HttpServletResponse response) throws Exception {
// response.sendRedirect("/index.jsp");
System.out.println("fun1 ...");
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
2.2 SpringMVC 接收请求数据
2.2.1 接收基本数据类型
@RequestMapping("/query")
public String query(@RequestParam(value = "ids",required = true,defaultValue = "123") Integer id, String name){
System.out.println("query ..... " + id + ":" + name);
return "/index.jsp";
}
2.2.2 接收对象
@RequestMapping("/save")
public String add(User user){
System.out.println("save ..... " + user);
return "/index.jsp";
}
2.2.3 接收数组
@RequestMapping("/delete")
public String delete(String[] loves){
System.out.println("delete ..... ");
if(loves !=null){
for(String l : loves){
System.out.println(l);
}
}
return "/index.jsp";
}
注意:在形参中我们不能够通过集合的方式来获取传递的参数。
在自定义对象中可以使用集合获取数组的形式来接收请求的参数。
2.3.4 自定义转换器
package com.gupaoedu.convert;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConvert implements Converter<String,Date> {
@Override
public Date convert(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
配置文件中注册
<?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
">
<!-- 开启扫描 -->
<context:component-scan base-package="com.gupaoedu.controller" />
<!-- 开启SpringMVC注解的使用 -->
<mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"/>
<!-- 配置转换器 -->
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"
id="formattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.gupaoedu.convert.DateConvert"/>
</set>
</property>
</bean>
</beans>
2.3 响应数据
2.3.1 ModelAndView 传递
@RequestMapping("/query")
public ModelAndView query(){
System.out.println("query ..... ");
ModelAndView mm = new ModelAndView();
mm.setViewName("/user.jsp");
mm.addObject("msg","msg.....");
return mm;
}
然后在 jsp 页面中通过 EL 表达式获取传递的信息。
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello<br>
${msg}
</body>
</html>
2.3.2 通过 Map 对象传值
@RequestMapping("/save")
public String add(Map<String,Object> map){
System.out.println("save ..... ");
map.put("msg","map ...msg");
return "/index.jsp";
}
2.3.3 通过 Model 来接收
@RequestMapping("/delete")
public String delete(Model model){
System.out.println("delete ..... ");
model.addAttribute("msg","model ...msg");
return "/index.jsp";
}
2.3.4 通过 ModelMap 响应数据
@RequestMapping("/update")
public String update(ModelMap mm){
System.out.println("update ..... ");
mm.put("msg","ModelMap ... msg");
return "/index.jsp";
}
三、Spring 集成开发篇
3.1 Spring 整合 SpringMVC
1. 修改【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.gupaoedu</groupId>
<artifactId>springmvc_demo_03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc_demo_03 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc_demo_03</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port> <!-- 访问端口 -->
<path>/</path> <!-- 访问路径 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
2. 新建【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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 添加扫描 -->
<context:component-scan base-package="com.gupao.edu" use-default-filters="true">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
3. 新建【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">
<!-- 添加扫描 -->
<context:component-scan base-package="com.gupao.edu"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 开启注解-->
<mvc:annotation-driven/>
</beans>
4. 配置【src/main/webapp/WEB-INF/web.xml】文件;
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置Spring -->
<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>
<!-- 配置SpringMVC -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联SpringMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. 新建【com.gupaoedu.service.IUserService】;
package com.gupaoedu.service;
public interface IUserService {
public String hello();
}
6. 新建【com.gupaoedu.service.impl.UserServiceImpl】;
package com.gupaoedu.service.impl;
import com.gupaoedu.service.IUserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements IUserService {
@Override
public String hello() {
return "hello Service ... ... ";
}
}
7. 新建【com.gupaoedu.controller.UserController】;
package com.gupaoedu.controller;
import com.gupaoedu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/user/hello")
public String hello() {
return userService.hello();
}
}
3.2 Spring 整合 MyBatis
1. 新建一个 Maven 项目,添加依赖;
<?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.gupaoedu</groupId>
<artifactId>springmvc_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- Mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Spring整合MyBatis的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- 日志框架 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<finalName>MobileSchool-chat</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
2. 新建【User】;
package com.gupaoedu.pojo;
public class User {
private Integer id;
private String userName;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
'}';
}
}
3. 新建【UserMapper.java】
package com.gupaoedu.mapper;
import com.gupaoedu.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> queryList();
}
4. 新建【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.gupaoedu.mapper.UserMapper">
<select id="queryList" resultType="com.gupaoedu.pojo.User">
SELECT * FROM USERS ORDER BY id DESC
</select>
</mapper>
5. 新建【IUserService】
package com.gupaoedu.service;
import com.gupaoedu.pojo.User;
import java.util.List;
public interface IUserService {
public List<User> queryList();
}
6. 新建【UserServiceImpl】
package com.gupaoedu.service.impl;
import com.gupaoedu.mapper.UserMapper;
import com.gupaoedu.pojo.User;
import com.gupaoedu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper mapper;
public List<User> queryList() {
return mapper.queryList();
}
}
7. 新建【db.properties】
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
8. 新建【log4j.properties】
log4j.rootCategory=DEBUG, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\\logs\\dpb.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
9. 新建【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 >
</configuration>
10. 新建【applicationContext.xml】
<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:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 添加扫描路径 -->
<context:component-scan base-package="com.gupaoedu.service.impl"/>
<!-- 关联属性文件 -->
<context:property-placeholder location="db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 和MyBatis的整合 将MyBatis相关的对象交给SpringIoC容器管理-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联MyBatis的配置文件 -->
<property name="configLocation" value="mybatis-config.xml"/>
<!-- 关联数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置扫描路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.gupaoedu.mapper"/>
</bean>
</beans>
11. 新建【AppTest】测试
package com.gupaoedu;
import com.gupaoedu.pojo.User;
import com.gupaoedu.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class AppTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService bean = ac.getBean(IUserService.class);
List<User> list = bean.queryList();
for(User user : list) {
System.out.println(user);
}
}
}