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
SSM(Spring4.x.x+SpringMVC4.x.x+Mybatis3.4.x)框架整合的更多相关文章
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- SSM三大框架整合详细教程
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
1.基本概念 1.1.Spring Spring 是一个开源框架, Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作 Expert O ...
- SpringMVC详解(四)------SSM三大框架整合之登录功能实现
为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis
原博主链接:( http://blog.csdn.net/zhshulin ) 使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么 ...
- 基于maven的ssm框架整合
基于maven的ssm框架整合 第一步:通过maven建立一个web项目. 第二步:pom文件导入jar包 (1 ...
随机推荐
- Windows下利用MKL加速caffe,与openblas比较
一.介绍:先简单Mark一下网上的介绍资料,弄清楚MKL是个啥,已经与openblas等的关系. 矩阵运算库blas, cblas, openblas, atlas, lapack, mkl之间有什么 ...
- ubuntu中如何安装python3.6
此处使用命令行方式来安装Python3.6: sudo wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz sudo ta ...
- 关于在虚拟机上安装ubuntu输入不了中文的问题
打开终端后,无法输入中文,按照网络上的教程 1.安装语言包 System Settings–>Language Support–>Install/Remove Languages 选中ch ...
- VS2008,System.Runtime.InteropServices.COMException (0x800401F3): Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
在VS2008环境中编译调试运行不报错,但在发布的exe文件运行就报错 System.Runtime.InteropServices.COMException (0x800401F3): Invali ...
- 【C++ 流类库与输入输出 】实验七
1. 基础练习 (1)教材习题 11-7 (2)教材习题 11-3 (3)教材习题 11-4 2. 应用练习 (1)已知有班级名单文件 list.txt(见实验 7 附件包).编写一个应用程序实现随机 ...
- css-不固定宽高定位
position: fixed; top:50%; left: 50%; transform: translate(-50%, -50%);
- javascript的数组之push()
push()方法讲一个元素或多个元素添加到数组的末尾,并返回新数组的长度length,修改数组自身. var numbers = [1, 2, 3]; numbers.push(4); console ...
- 删除U8中单据已经审核完成但工作流未完成的任务
问题描述: U8工作流终审结点后面还有节点时,当终审终点完成后,系统会通知后面的节点进行审核,但单据显示已经审核完成,后面的节点无法审核,工作任务会一直挂着,无法删除. 例如下图中,节点"终 ...
- jQuery实现图片懒加载
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ASM: Active Shape Models--Their Training and Application
这篇论文的前半部分基本就是论文<Training Models of Shape from Sets of Examples>的全部内容,只不过多两个应用示例,后半部分在PDM模型的基础上 ...