建立springmvc项目,先跑起来,再整合spring和mybatis
一.SpringMVC建立
1.新建maven工程,安装tomcat
2.导入pom
<!-- springmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
3.webapp下建立如下文件
web-inf的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>spring_maven</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>ssm1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,
名称为[<servlet-name>]-servlet.xml,此处不定义,则为ssm1.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.main/resources下建立,如下三个xml文件
分别为mybatis的配置文件,spring的配置文件,springmvc的配置文件,此处暂时只写springmvc-servlet的内容,剩下两个等说到的时候再填充。springmvc-servlet的内容为:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
<context:component-scan base-package="com.bd"/>
<!-- 开启注解 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/views/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!--释放静态资源 -->
<mvc:default-servlet-handler/>
</beans>
到此为止,springmvc已经完成,此时可以跑一下springmvc,看是否出现问题
springmvc的测试
1.在main/java下建立以下四个包
并在controller包中建立一个UserController类,用于测试springmvc
pojo:实体类(用于映射数据库中的字段)
dao:数据访问层(规定接口,用于定义访问数据库中的方法)
service:操作数据访问层,返回处理后的数据给controller层
controller:控制器(功能类似于servlet,位于web层和service层之间)
UserController.java:
package com.bd.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@RequestMapping("/test")
public ModelAndView test1() {
System.out.println("this is userTest");
return null;
}
}
2. webapp/index.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test">To test</a>
</body>
</html>
3.测试结果如下
二.集成spring
1.导入pom文件
由于maven会自动导入相关联的包,在导入spring-mvc的jar包的时候,spring的基本包都已经集成上去,故此处不需要导入
2.扩充web.xml中的内容,将spring继承进web.xml中
<!-- 加载spring核心文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!--中文乱码 -->
<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>
3.在spring-context.xml中开启注解,spring-context.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解自动生成bean -->
<context:component-scan base-package="com.bd"/>
</beans>
spring的集成暂时完成,等下需要将mybatis集成进spring中时,再进一步进行配置
测试spring
1.在main/java下建立UserService.java
package com.bd.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void test() {
System.out.println("this is UserService");
}
}
将UserController中的内容更改为
package com.bd.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bd.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/test")
public ModelAndView test1() {
System.out.println("this is userTest");
userService.test();
return null;
}
}
测试结果:
此时spring已经成功集成,现在只剩下最后的mybatis
三.MyBatis的集成
1.导入pom.xml文件
<!-- spring-mybatis整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mybatis相关包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!-- 导入mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
2.在spring中整合mybatis,即在spring-context.xml添加如下内容
<!--加载外界文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 读取mybatis文件 -->
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis和数据库连接 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!--读取mybatis的总配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 类型别名扫描包 -->
<property name="typeAliasesPackage" value="com.bd.pojo"></property>
<!--配置文件和接口分离 -->
<property name="mapperLocations" value="classpath:*/*.xml"></property>
</bean>
<!--mapper.xml -->
<!-- 配置接口-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.bd.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
2.1 在dao包和pojo包下分别建立UserDao.java接口和User.java类(注意,一个是接口,一个是类)
UserDao.java
package com.bd.dao;
import com.bd.pojo.User;
public interface UserDao {
User getUser(String username);
}
User.java
package com.bd.pojo;
public class User {
int id;
String username;
String password;
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
2.2 新建一个mapper文件夹,在里面配置UserDao的实现:UserDao.xml,并在main/resources下新建jdbc.properties文件
UserDao.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.bd.dao.UserDao">
<select id="getUser" resultType="User">
select * from user where username=#{username}
</select>
</mapper>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456
3.配置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>
此时编译一下,启动servlet,看是否有问题,若问题,此时最基本的ssm框架已经搭建成功,简单调试一下即可
Mabatis测试
1.建立test数据库,并添加一张user表,如下:
2.修改UserService中的代码
package com.bd.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bd.dao.UserDao;
import com.bd.pojo.User;
@Service
public class UserService {
@Autowired
UserDao userDao;
public void test() {
User user=userDao.getUser("aaa");
System.out.println(user);
}
}
3.测试结果如下:
四.其他
1.mybatis.config实现分页,开启二级缓存,开启驼峰法
<?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>
<!-- 驼峰法 只能是数据库是带下划线的时候可以匹配pojo不带下划线的字段 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
2.log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss,SSS} %m%n
3.添加事务(spring-context.xml添加下列代码)
<!-- 数据源事务管理平台 可以管理不同的数据库 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入相应的数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解标签 -->
<tx:annotation-driven/>
并在service层中需要加强的函数上,添加
@Transactional
4.pom文件
<dependencies>
<!--jstl、servlet-api、-->
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- Jackson Json处理工具包 springmvc 处理json数据的 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.2</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 日志包依赖 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>3.8.2</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin </artifactId>
<configuration>
<!-- 项目运行后使用的端口号 -->
<port>80</port>
<!-- 项目访问路径 -->
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>