环境搭建在此不提
基本概念在此不提
1、新建Maven Web项目
- 使用IDEA新建项目,选择Maven–create from archetype–maven-archetype-webapp
- 填写GroupId、ArtifactId
- 选择Maven地址、settings文件、本地仓库地址
- 选择项目创建路径
- 完成创建
在main目录下与resources、webapp同级目录创建java目录并将其标注为Source folders
2、引入所需jar包
为方便 将基本所有能用到的dependency粘贴在这里
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<!--mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
</dependency>
<!--打印日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
等待Maven将以上所有Jar包全部引入后即可使用,提一句,如果Java环境是1.8时,Spring所有Jar包依赖的版本最好直接在4.0以上,否则Spring 3.x和JDK8有兼容性问题,会报错。
3、整合SSM框架
3.1 整合Spring+Mybatis
3.1.1创建jdbc.properties及log4j.xml两个配置文件
jdbc.properties
#驱动地址
driver=com.mysql.jdbc.Driver
#数据库连接地址
url=jdbc:mysql://127.0.0.1:3306/db_test
#用户名
username=root
#密码
password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
log4j.xml
我只想在控制台看到输出的sql语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- appender -->
<appender name="console_appender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n" />
</layout>
</appender>
<logger name="sysout" additivity="true">
<level value="ERROR" />
<appender-ref ref="console_appender" />
</logger>
<!--<logger name="sqlLog" additivity="true">-->
<!--<level value="debug" />-->
<!--<appender-ref ref="console_appender" />-->
<!--</logger>-->
<logger name="com.test.sjz.mapper" >
<level value="DEBUG" />
<appender-ref ref="console_appender" />
</logger>
<root>
<level value="ERROR"/>
<appender-ref ref="console_appender"/>
</root>
</log4j:configuration>
3.1.2 创建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">
<!-- 自动扫描 -->
<context:component-scan base-package="com.test.sjz" />
<!-- 引入配置文件 -->
<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 name="maxActive" value="${maxActive}" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}" />
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/test/sjz/mapper/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.sjz.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
3.1.3 使用JUnit做测试
3.1.3.1 创建数据库、表(在此使用以前的表)
3.1.3.2 利用Mybatis Generator自动生成代码
参考博客(还没写)(因为是简单学搭建,所以采用自动生成代码,过后可能会单写一篇博客关于如何自己手写代码)
3.1.3.3 编写Service接口及实现
Service目录在src中java文件夹下的包结构下,实现放在Service包中的impl中
NewsService.java
package com.test.sjz.service;
import com.test.sjz.entity.NewsWithBLOBs;
/**
* 新闻Service层接口
* @author 单继重
* @since 2017/8/31 16:44
*/
public interface NewsService {
/**
* 根据主键ID查询出一条新闻
* @param id 主键ID
* @return 新闻实体类
*/
public NewsWithBLOBs queryById(int id);
}
NewsServiceImpl.java
package com.test.sjz.service.impl;
import com.test.sjz.entity.NewsWithBLOBs;
import com.test.sjz.mapper.NewsMapper;
import com.test.sjz.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 新闻Service层实现
* @author 单继重
* @since 2017/8/31 16:45
*/
@Service("newsService")
public class NewsServiceImpl implements NewsService {
@Autowired
private NewsMapper newsMapper;
public NewsWithBLOBs queryById(int id) {
return newsMapper.selectByPrimaryKey(id);
}
}
3.1.3.4 创建测试类
首先在src中创建与main文件夹同级test文件夹,并在test目录下创建java文件夹并将其Mark as test source folders
在java目录下创建与Service接口路径相同的包结构,并创建TestNewsService.java
package com.test.sjz.service;
import com.test.sjz.entity.NewsWithBLOBs;
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 java.util.logging.Logger;
/**
* 新闻Service层测试类
* @author Bryan
* @since 2017/8/31 16:47
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:spring-mybatis.xml"})
public class TestNewsService {
private static Logger logger = Logger.getLogger(String.valueOf(TestNewsService.class));
@Autowired
private NewsService newsService;
@Test
public void testQueryById() {
NewsWithBLOBs news = newsService.queryById(1);
// System.out.println(user.getUserName());
logger.info("值:"+news.getTitle());
System.out.println(news);
}
}
运行该方法,测试成功
[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from URL [file:/F:/Shanjz/Test/target/classes/spring-mybatis.xml]
[org.springframework.context.support.GenericApplicationContext] - Refreshing org.springframework.context.support.GenericApplicationContext@7921b0a2: startup date [Thu Aug 31 19:29:18 CST 2017]; root of context hierarchy
[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties]
八月 31, 2017 7:29:19 下午 com.test.sjz.service.TestNewsService test1
信息: 值:震惊
com.test.sjz.entity.NewsWithBLOBs@73e9cf30
[org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@7921b0a2: startup date [Thu Aug 31 19:29:18 CST 2017]; root of context hierarchy
说明Mybatis和Spring整合成功
未完待续