Spring Springmvc Mybatis--ssm的整合
一、搭建环境sts工具:
根据上图创建springproject
上图为项目结构 在此基础上进行搭建ssm项目。
二、添加依赖对pom文件添加坐标依赖:
xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.cngroupId>
<artifactId>ssmartifactId>
<name>SSMProjectname>
<packaging>warpackaging>
<version>1.0.0-BUILD-SNAPSHOTversion>
<properties>
<java-version>1.6java-version>
<org.springframework-version>3.1.1.RELEASEorg.springframework-version>
<org.aspectj-version>1.6.10org.aspectj-version>
<org.slf4j-version>1.6.6org.slf4j-version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-rabbitartifactId>
<version>1.3.5.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>2.2.2version>
<exclusions>
<exclusion>
<groupId>c3pogroupId>
<artifactId>c3poartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>3.0.4version>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.6version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.1version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>2.2.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.mongodbgroupId>
<artifactId>mongo-java-driverartifactId>
<version>3.0.2version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-mongodbartifactId>
<version>1.8.4.RELEASEversion>
<exclusions>
<exclusion>
<groupId>org.mongodbgroupId>
<artifactId>mongo-java-driverartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.1.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.12version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.37version>
dependency>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-coreartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-webartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-springartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-codecgroupId>
<artifactId>commons-codecartifactId>
<version>1.10version>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.8.0version>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>net.sf.ezmorphgroupId>
<artifactId>ezmorphartifactId>
<version>1.0.6version>
dependency>
<dependency>
<groupId>org.apache.lucenegroupId>
<artifactId>lucene-coreartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>org.apache.lucenegroupId>
<artifactId>lucene-analyzers-commonartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>org.apache.lucenegroupId>
<artifactId>lucene-analyzers-smartcnartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>org.apache.lucenegroupId>
<artifactId>lucene-queryparserartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>org.apache.lucenegroupId>
<artifactId>lucene-highlighterartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.16version>
dependency>
<dependency>
<groupId>org.jsoupgroupId>
<artifactId>jsoupartifactId>
<version>1.8.3version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.modulegroupId>
<artifactId>jackson-module-jaxb-annotationsartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrsgroupId>
<artifactId>jackson-jaxrs-json-providerartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.9version>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-websocketartifactId>
<version>4.0.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-messagingartifactId>
<version>4.0.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.jsoupgroupId>
<artifactId>jsoupartifactId>
<version>1.8.3version>
dependency>
dependencies>
<build>
<plugins>
<plugin><artifactId>maven-eclipse-pluginartifactId>
<version>2.9version>
<configuration>
<additionalProjectnatures><projectnature>org.springframework.ide.eclipse.core.springnatureprojectnature>
additionalProjectnatures>
<additionalBuildcommands><buildcommand>org.springframework.ide.eclipse.core.springbuilderbuildcommand>
additionalBuildcommands>
<downloadSources>truedownloadSources>
<downloadJavadocs>truedownloadJavadocs>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>2.5.1version>
<configuration>
<source>1.6source>
<target>1.6target><compilerArgument>-Xlint:allcompilerArgument>
<showWarnings>trueshowWarnings>
<showDeprecation>trueshowDeprecation>
configuration>
plugin>
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>exec-maven-pluginartifactId>
<version>1.2.1version>
<configuration>
<mainClass>org.test.int1.MainmainClass>
configuration>
plugin>
plugins>
build>
project>
以上依赖包含了所有能用到坐标 例如 redis、mongodb、websocket、poi、rabbitmq、mybatis、spring等依赖坐标。
//会需要很长时间下载相关依赖可根据自身情况选择依赖
三、开始正式搭建项目:
对Java代码进行分包处理。
四、配置配置文件:
创建applicationContext.xml,mybatis-config.xml
1、根据Spring官网粘贴头文件以及相关连接。
2、配置数据源来连接数据库。
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/tiangou?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/cn/ssm/dao/*.xml">property>
<property name="configLocation" value="classpath:mybatis-config.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.ssm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8">property>
<property name="maxUploadSize" value="10485760000000000">property>
<property name="maxInMemorySize" value="40960">property>
<property name="resolveLazily" value="true">property>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
<tx:method name="load*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.cn.ssm.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
aop:config>
<context:component-scan base-package="com.cn.ssm.service" />
beans>
五、配置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>
<typeAliases>
<package name="com.cn.ssm.entity"/>
typeAliases>
configuration>
六.配置spring-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=
"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<mvc:annotation-driven/>
<mvc:resources location="/" mapping="/**/*.html"/>
<mvc:resources location="/" mapping="/**/*.js"/>
<mvc:resources location="/" mapping="/**/*.css"/>
<mvc:resources location="/" mapping="/**/*.jpg"/>
<mvc:resources location="/" mapping="/**/*.gif"/>
<mvc:resources location="/" mapping="/**/*.gzcss"/>
<mvc:resources location="/" mapping="/**/*.gzjs"/>
<mvc:resources location="/" mapping="/**/*.png"/>
<mvc:resources location="/" mapping="/**/*.eot"/>
<mvc:resources location="/" mapping="/**/*.svg"/>
<mvc:resources location="/" mapping="/**/*.ttf"/>
<mvc:resources location="/" mapping="/**/*.woff"/>
<mvc:resources location="/" mapping="/**/*.woff2"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp">property>
bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="10000000"/>
bean>
<context:component-scan base-package="com.cn.ssm.controller" />
beans>
项目结构:
七:业务逻辑:
Controller的编写:
@Controller
public class HomeController {
/**
* 登录的方法
* 通过用户名查询数据库的密码来比对输入的密码
* @return
*/
@Autowired
private CheckSelf checkSelf;
@RequestMapping(value="/login",method=RequestMethod.GET, produces ="application/json; charset=utf-8")
@ResponseBody
public String login(@RequestParam(value ="username", required =false) Stringname,
@RequestParam(value="password",required=false,defaultValue="1234567") String code){
if (checkSelf.checkSelf(name,code)) {
return"suucess";
}
return"登录失败";
}
}
Entity的使用和数据库一一对应
public class Customer {
private String customerId;
private String name;
private int age;
private String mobile;
private String password;
private String url;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(StringcustomerId) {
this.customerId =customerId;
}
public String getName() {
return name;
}
public void setName(Stringname) {
this.name =name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age =age;
}
public String getMobile() {
return mobile;
}
public void setMobile(Stringmobile) {
this.mobile =mobile;
}
public String getPassword() {
return password;
}
public void setPassword(Stringpassword) {
this.password =password;
}
public String getUrl() {
return url;
}
public void setUrl(Stringurl) {
this.url =url;
}
}
Dao层的编写:
一、mapperJava类
public interface CustomerMapper {
public String findByName(Stringname);
}
二、mapper.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.cn.ssm.dao.CustomerMapper">
<resultMap type="Customer" id="CustomResult">
<id property="customerId" column="CUSTOMERID" />
<result property="name" column="NAME" />
<result property="mobile" column="MOBILE" />
<result property="age" column="AGE" />
<result property="password" column="PASSWORD" />
<result property="url" column="URL" />
resultMap>
<select id="findByName" parameterType="String" resultType="String">
select PASSWORD from customer
<where>
AND NAME=#{name}
where>
select>
mapper>
Service的编写使用
一、接口的编写:
public interface CheckSelf {
/**
* 判断用户名密码是否正确
* @param name
* @param code
* @return
*/
public boolean checkSelf(Stringname,Stringcode);
}
二、实现的编写
@Service
public class CheckSelfImplimplements CheckSelf {
@Autowired
private CustomerMappercustomerMapper;
@Override
public boolean checkSelf(Stringname, Stringcode) {
// TODO Auto-generated method stub
String password = customerMapper.findByName(name);
if (code.equals(password)) {
return true;
}
return false;
}
}
最后项目的结构:
数据库自己创建就简单的一个表:
启动项目发布tomcat访问路径:
http://localhost:8080/ssm/login?username=zhangchao&&password=122
http://localhost:8080/ssm/login?username=zhangchao&&password=123