搭建步骤
一、创建Maven Web项目
依照个人习惯自行创建。先新建一个Maven工程,再添加web支持
可参考博客:http://www.cnblogs.com/wql025/p/5205716.html
工程目录如图:
完整的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.0modelVersion>
<groupId>com.zhou.ssmgroupId>
<artifactId>ssmTestartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<spring.version>4.3.1.RELEASEspring.version>
<mybatis.version>3.4.5mybatis.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.5version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.35version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>net.sf.json-libgroupId>
<artifactId>json-libartifactId>
<version>2.4version>
<classifier>jdk15classifier>
dependency>
<dependency>
<groupId>commons-httpclientgroupId>
<artifactId>commons-httpclientartifactId>
<version>3.1version>
dependency>
<dependency>
<groupId>javax.validationgroupId>
<artifactId>validation-apiartifactId>
<version>1.1.0.Finalversion>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.4version>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.9version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
project>
二、配置SpringMVC及测试
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.10version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:resources mapping="/static/**" location="/static/"/>
<context:component-scan base-package="com.zhou.ssm"/>
<mvc:annotation-driven />
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>springMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
<async-supported>trueasync-supported>
servlet>
<servlet-mapping>
<servlet-name>springMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<body>
Controller --> index
body>
html>
java包下,新建com.zhou.ssm包,目录下新建controller包
新建HelloController.java
@Controller
public class IndexController {
@RequestMapping(value="/", method= RequestMethod.GET)
public String index(){
return "index";
}
}
配置tomcat运行测试。
SpringMVC配置简单可参考:
http://www.cnblogs.com/yanyd/p/4266179.html
http://www.cnblogs.com/winner-0715/p/5294917.html
超详细可参考:http://blog.csdn.net/qq_27093465/article/details/68961393
三、Spring+Mybatis配置及连接数据库测试
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456
spring-mybatis.xml
<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="typeAliasesPackage" value="com.zhou.ssm">property>
<property name="mapperLocations" value="classpath:/mapper/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
<property name="basePackage" value="com.zhou.ssm">property>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
bean>
<context:component-scan base-package="com.zhou.ssm">
<context:exclude-filter type="aspectj" expression="com.ipu.pushMessage.mapper.*"/>
context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true">aop:aspectj-autoproxy>
beans>
在web.xml中添加:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mybatis.xmlparam-value>
context-param>
bean包,新建Media.java类, mysql建表
public class Media {
public String media_id;
public String type;
public String created_time;
public String status;
public String getMedia_id() {
return media_id;
}
public void setMedia_id(String media_id) {
this.media_id = media_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCreated_time() {
return created_time;
}
public void setCreated_time(String created_time) {
this.created_time = created_time;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
mapper包下新建MediaMapper接口
@Repository
public interface MediaMapper {
public int saveMedia(Media media);
public List queryAll();
}
service包下,新建MediaService接口
public interface MediaService {
public int saveMedia(Media media);
public List queryAll();
}
impl包下,新建MediaServiceImpl类实现MediaService接口
@Service
public class MediaServiceImpl implements MediaService{
@Autowired
private MediaMapper mediamapper;
@Override
public int saveMedia(Media media){
return mediamapper.saveMedia(media);
}
@Overside
public List queryAll(){
return mediamapper.queryAll();
}
}
配置文件resources包下的mapper包,新建MediaMapper.xml
增删改查详解参考:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete
<mapper namespace="com.ipu.pushMessage.mapper.MediaMapper">
<insert id="saveMedia" parameterType="Media">
insert into psg_wechat_media(TYPE, MEDIA_ID, CREATED_TIME, STATUS) VALUES(#{type}, #{media_id}, #{created_time}, #{status})
insert>
<select id="queryAll" resultType="Media">
select * from psg_wechat_media
select>
mapper>
添加测试类:
public class TestSpringMyBatis {
@Test
public void saveMedia() {
System.out.println("start-------------------------");
//初始化容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-mybatis.xml");
//获得bean
MediaMapper mediaMapper = ctx.getBean(MediaMapper.class);
//访问数据库
Media media = new Media();
media.setCreated_time("2017-01-01 10:23:23");
media.setMedia_id("80222");
media.setStatus("0");
media.setType("thume");
mediaMapper.saveMedia(media);
System.out.println("end----------------------------");
}
}
public class QueryAllMedia {
@Test
public void queryallmedia(){
System.out.println("start-------------------------");
//初始化容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-mybatis.xml");
//获得bean
MediaMapper mediaMapper = ctx.getBean(MediaMapper.class);
List medialist = mediaMapper.queryAll();
for(Media i: medialist){
System.out.println("media: "+i.getMedia_id());
System.out.println("type: "+i.getType());
System.out.println("created_time: "+i.getCreated_time());
System.out.println("status: "+i.getStatus());
System.out.println();
}
System.out.println(medialist);
}
}
右键运行测试类。
参考博客:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral