SSM(Spring4.x.x+SpringMVC4.x.x+Mybatis3.4.x)框架整合

本文是参考SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)修改而来的

一、环境

  1. Myeclipse2016

  2. Mysql

二、具体步骤

  1. 整合Spring和Mybatis

    1. 导入所需要的包(需要的包都在后边下载链接里有)

    2. 建立jdbc.properties文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

    3. 建立spring-mybatis.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:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描bean -->
<context:component-scan base-package="com" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 扫描mapper.java -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper" />
</bean>
<!-- 扫描配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
</bean> </beans>

4. 建立mybatis-config.xml,这个文件是用来显示SQL语句的

<?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>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>

5. log4j配置文件

log4j.rootLogger=INFO,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = logs/ssm.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

6. 新建user表

CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

7. 使用mybatis的自动生成mapper和model

参考博文:http://blog.csdn.net/zhshulin/article/details/23912615

具体根据我的代码里的注释去操作

8. 创建userService接口,并实现

userServiceImpl类,这里UserMapper上有一个自动装配的注解,UserServiceImpl上有一个Service注解,在后边测试的时候就可以直接拿来用,而不用去配置文件写

package com.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.mapper.UserMapper;
import com.model.User;
import com.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(int id) {
return this.userMapper.selectByPrimaryKey(id);
} }

9. Junit测试

TestMybatis类,

@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})表示加载spring-mybaits配置文件
package com.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.model.User;
import com.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TestMybatis{
@Autowired
private UserService userService; @Test
public void test1(){
User user = userService.getUserById(1);
System.out.println(user);
}
}

剩下Spring与SpringMVC的整合,上边那边博文已经讲的很详细了。具体的可以看代码

我的代码是在他的代码基础上修改的;他的代码好像有点问题,运行不了。

代码:http://download.csdn.net/detail/a781675302/9822792

上一篇:Apple Swift编程语言入门中文教程


下一篇:JPA的初级CRUD-01