关于Spring的核心理念和Mybatis的优点网上已经有很多文档做了说明。这篇博客,只记录springmvc整合mybatis时常见的知识点,以及注意事项,它只有最精简的几个模块,以帮助初学者迅速搭建框架,建立一个比较对ssm框架比较全面的认识
Eclipse 搭建Maven web项目
- File-->New-->Maven Project
*Next
*选择maven webapp项目 点击next
- 填好maven项目坐标 groupId,artifactId,点击next
*初始项目目录结构如下
开始时会有报错,是应为没有javax.servlet-api.jar,待会儿在pom.xml中添加相关依赖就行了
*在main目录下添加java目录,在src目录下添加test目录,在test目录下添加java目录
因为在build path-->configure build path 后进入的这个页面中,可以看见少了这2个目录
*在pom.xml文件中添加相关依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ws</groupId>
<artifactId>SSM</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SSM Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.2.RELEASE</spring.version>
<!-- <mysql-connector-java.version>5.1.34</mysql-connector-java.version> -->
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<!-- mybatis start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- mybatis end -->
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<!-- 数据库连接池,这里我们的项目并没有使用这个连接池,使用的是spring jdbc中的一个连接池,不同的连接池,
在配置数据源的时候,属性名可能是不同的,需要自己查看先关的类,比如有的驱动名是 driverClass 有的是 driverClassName -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>SSM</finalName>
</build>
</project>
- 然后配置web.xml文件
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<!-- J2EE容器添加的上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义spring mvc的配置文件名称和路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- spring mvc 请求后缀 -->
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 在main-->resources目录下添加spring-servlet.xml文件,用来管理springmvc的配置
<?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: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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<mvc:annotation-driven />
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.ws.controller" />
<!-- 配置试图渲染器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
- 在main-->resources目录下添加jdbc.properties 配置文件
这里有一些需要注意的地方,一是结尾不要有空格,二是设置用户名时不要用username,因为可能会跟系统的其他配置有冲突,我就是因为这个问题,找了很久错误
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
user=root
password=123456
*在main-->resources目录下添加applicationContext.xml文件,用来管理spring bean对象,及整合mybatis
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 引入jdbc配置文件,在下文中就可以通过${name}来使用值 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定mapper xml文件的路径 你可以放在resources目录下,然后通过classpath来引入 -->
<property name="mapperLocations" value="classpath:mapping/*.xml" />
</bean>
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper ,mybatis会自动通过xml映射文件,生成相应的代码,然后通过mapper 接口来调用相关的操作-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ws.mapper" />
</bean>
<!-- 可通过注解控制事务 -->
<tx:annotation-driven />
</beans>
- 在java目录下新建如下几个包
*在bean中加入User类
package com.ws.bean;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
*在mapper中加入UserMapper 接口
package com.ws.mapper;
import org.springframework.stereotype.Repository;
import com.ws.bean.User;
@Repository
public interface UserMapper{
public User getUserInfoByName(String name);
}
*在resources目录下添加mapping文件夹,并创建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" >
<!-- 这块等于dao接口的实现,namespace必须和接口的类路径一样 -->
<mapper namespace="com.ws.mapper.UserMapper">
<!-- mapper接口中的函数应该和id所设置的相同,如果没有为自定义类型设置别名,则需要使用完整的包加类名 -->
<select id="getUserInfoByName" parameterType="String" resultType="com.ws.bean.User">
select * from user where name=#{name}
</select>
</mapper>
*将index.jsp文件改成如下
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h2>Hello World!</h2>
<a href="test.action?name=ws">点击跳转</a>
</body>
</html>
*在WEB-INF目录下新建一个jsp目录,并创建一个User.jsp,内容如下
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
Hello ${user.name } ${user.age }
</body>
</html>
*在controller包中添加如下Test 控制器 ,代码如下
package com.ws.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ws.bean.User;
import com.ws.mapper.UserMapper;
import javax.annotation.Resource;
@Controller
public class Test {
@Autowired
private UserMapper userService;
@RequestMapping(value="test")
public String test(@RequestParam("name") String name ,Model model){
User user=null;
user=userService.getUserInfoByName(name);
model.addAttribute(user);
return "User";
}
}
-
在mysql中创建名为mybatis 的数据库和user表
create database mybatis;
use mybatis;
create table user(
name varchar(255),
age int
);
insert into user values("ws",21); 至此整个框架已经搭建完成,放在tomcat中运行
这知识一个最小的模快,后续还会添加更多的功能