1、新建Maven项目创建pom.xml
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssm.demo2</groupId>
<artifactId>ssm_demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-oxm</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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId>
</dependency> -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 配置maven编译插件,及编译的 jdk版本 -->
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.1新建mybatis数据库 utf-8格式 并新建user数据表
建表语句:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` date DEFAULT NULL COMMENT '生日',
`sex` char(1) DEFAULT NULL COMMENT '性别',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
2、新建Mybatis配置文件
在src/main/resources创建mybatis目录再新建mybatis_config.xml
文件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>
<!-- 加载数据源配置 -->
<properties resource="db.properties" />
<!-- 别名 -->
<typeAliases>
<typeAlias alias="User" type="com.ssm.demo2.po.User" />
</typeAliases>
<!--配置jdbc -->
<!-- <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.jdbcUrl}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> -->
<mappers>
<!-- 加载mapper -->
<!-- <mapper resource="mybatis/UserMapper.xml" /> -->
<!--
注解的方式
自动扫描 加载mapper
但是mapper中.xml 必须与接口名字一样
-->
<package name="com.ssm.demo2.mapper" />
</mappers>
</configuration>
3、创建db.properties与log4j.properties
db.properties内容如下:
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=utf8
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
log4j.properties内容如下:
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4、新建Spring配置文件,管理数据源
在src/main/resources新建spring目录再创建applicationContext-dao.xml
在applicationContext-dao.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--注解扫描器 -->
<context:annotation-config />
<context:component-scan base-package="com.ssm.demo2">
</context:component-scan>
<!-- 加载数据源配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dataSource 使用dbcp连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.jdbcUrl}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!-- 设置数据库连接池的最大连接数 -->
<property name="maxActive" value="${jdbc.maxPoolSize}" />
<!-- 设置数据库连接池的初始化连接数 -->
<property name="initialSize" value="${jdbc.initPoolSize}" />
</bean>
<bean
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池 -->
<property name="dataSource" ref="dataSource" />
<!--configLocation属性指定mybatis的核心配置文件 -->
<property
name="configLocation" value="classpath:mybatis/mybatis_config.xml" />
</bean>
<!--注册UserMapper映射 -->
<!-- <bean
id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
sqlSessionFactory属性指定要用到的SqlSessionFactory实例
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象
<property name="mapperInterface" value="com.ssm.demo2.mapper.UserMapper"/>
</bean> -->
<!-- mybatis自动扫描包下的mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.demo2.mapper" />
<!-- optional unless there are multiple session factories defined -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 数据库的事务管理器配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
5、新建Springmvc配置文件applicationContext-mvc.xml
applicationContext-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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 使用注解配置 -->
<mvc:annotation-driven />
<!--自动扫描包 -->
<context:component-scan base-package="com.ssm.demo2.controller" />
<!-- ViewResolver 视图解释器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property
name="prefix"
value="/WEB-INF/jsp/" />
<property
name="suffix"
value=".jsp" />
</bean>
</beans>
6、web.xml配置
在src/main/webapp目录下新建WEB-INF新建jsp目录新建web.xml,
文件头从要运行的tomcat目录复制过来!
web.xml内容如下:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0" >
<!-- Spring应用上下文, 理解层次化的ApplicationContext ,
applicationContext-*.xml这里用了通配符,
这里只要与spring整合的文件符合这样的格式将自动全部加载
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>SSMDemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SSMDemo</servlet-name>
<!-- mvc-dispatcher拦截所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--forwarding 跳转的servlet -->
<servlet>
<servlet-name>forwarding</servlet-name>
<servlet-class>com.spring.mvcdemo.servlet.ForwardingServlet</servlet-class>
</servlet>
<!--配置文件编码 -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring 全局上下文的 监听,当配置必须有 applicationContext.xml文件,否则报错 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
7、新建测试例子:
在src/main/java目录下
新建包
com.ssm.demo.controller
com.ssm.demo.mapper
com.ssm.demo.po
com.ssm.demo.service
Com.ssm.demo.service.impl
在com.ssm.demo.po包下建 User.java
代码如下:
public class User {
private int id;
private String username;
private Date password;
private char sex;
private String address;
...getter/setter省略
}
在com.ssm.demo.controller包下建 UserController.java
代码如下:
@Controller
@RequestMapping("/user")
public class UserController {
/*@Autowired
UserMapper userMapper;*/
@Autowired
UserService userServie;
@RequestMapping("/list")
public ModelAndView userlist() throws Exception{
// List<User> users=userMapper.findUserList("王");
List<User> users=userServie.findUser("刘");
ModelAndView mView=new ModelAndView();
mView.addObject("users",users);
mView.setViewName("list");//返回的jsp页面名称
return mView;
}
}
在com.ssm.demo.mapper包下建 UserMapper.java与UserMapper.xml
UserMapper.java
public interface UserMapper {
public List<User>findUserList(String name) throws Exception;
}
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.ssm.demo2.mapper.UserMapper">
<!--查找记录 -->
<select id="findUserList" parameterType="java.lang.String" resultType="User">
select * from user where username like "%"#{value}"%"
</select>
</mapper>
在com.ssm.demo.service包下建UserService.java
UserService.java
public interface UserService {
public List<User>findUser(String name) throws Exception;
}
在com.ssm.demo.service.impl包下建UserServiceImpl.java
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper; //spring框架配置了注解,自动注入
@Override
public List<User> findUser(String name) throws Exception {
// TODO Auto-generated method stub
List<User> users=userMapper.findUserList(name);
return users;
}
在WEB-INF下新建jsp目录创建list.jsp
list.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>
<body>
<h2>SSM 部署成功! </h2>
<p>用户名:</p>
<h3>${users}</h3>
</body>
</html>
部署ok!