maven_SSM集成的demo
一、集成spring
二、集成springMVC
三、集成mybatis
1. pom.xml
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ddh.test</groupId>
<artifactId>maven_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>maven_ssm Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<srping.version>4.0.2.RELEASE</srping.version>
<mybatis.version>3.2.8</mybatis.version>
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <!-- java ee包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- spring框架包 start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${srping.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${srping.version}</version>
</dependency>
<!-- spring框架包 end -->
<!-- mybatis框架包 start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mybatis框架包 end -->
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- jstl标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log END -->
<!-- Json -->
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 start -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- 上传组件包 end -->
</dependencies> <build>
<finalName>maven_ssm</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2. web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app >
<display-name>Archetype Created Web Application</display-name>
<!--1.1配置spring初始化路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--1.2监听器的方式,加载spring,这就是spring和tomcat的接口-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--配置springmvc的核心控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--!!指定springMVC的配置文件-->
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--启动级别,和服务器一起启动-->
<load-on-startup>1</load-on-startup>
</servlet> <!--配置核心控制器映射,配置/意思是所有url都通过此servlet-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3. applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--扫描service-->
<context:component-scan base-package="com.daydayhave.ssm.service"/>
<bean id="date" class="java.util.Date" /> <!--引入mybatis-->
<import resource="applicationContext-mybatis.xml" /> <!-- 开启spring对事务的注解支持 -->
<tx:annotation-driven/> </beans>
4. applicationContext-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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!--扫描包 配置controller层的包扫描-->
<context:component-scan base-package="com.daydayhave.ssm.web.controller"/> <!--静态资源放行-->
<mvc:default-servlet-handler/>
<!--开启springmvc注解支持-@RequestMapping-->
<mvc:annotation-driven/> <!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<!-- spring el写法:5MB -->
<value>#{1024*1024*5}</value>
</property>
</bean> <!--视图解析器:前后缀配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀配置 -->
<property name="prefix" value="/WEB-INF/jsps/" />
<!-- 后缀配置 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
5. applicationContext-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: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.xsd
"> <!--jdbc.properties datasoruce(dbcp) SqlSessionFatory mapper servce junit 事务处理 web-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--连接数据4个属性 -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="150" />
<!--minIdle: 最小空闲连接 -->
<property name="minIdle" value="5" />
<!--maxIdle: 最大空闲连接 -->
<property name="maxIdle" value="20" />
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="30" />
<!-- 用来配置数据库断开后自动连接的 -->
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true" />
<!--removeAbandoned: 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10" />
<!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
<property name="maxWait" value="1000" />
<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="SELECT NOW() FROM DUAL" />
</bean> <!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库配置-->
<property name="dataSource" ref="dataSource" />
<!--别名就是mapper.xml中,不用谢类的全称,直接写 -->
<property name="typeAliasesPackage" value="com.daydayhave.ssm.domain,com.daydayhave.ssm.query" />
<!--映射文件-->
<property name="mapperLocations" value="classpath:com/daydayhave/ssm/mapper/*Mapper.xml" />
</bean> <!--扫描一个特定包,给该包下面所有的接口都产生一个以上的配置就ok
扫描后 框架 会给这个包下的所有的mapper接口生成实现-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.daydayhave.ssm.mapper" />
</bean> </beans>
6. jdbc.properties
jdbc.driverClassName = com.mysql.jdbc.Driver
#company
#house
jdbc.url=jdbc:mysql:///testssm
jdbc.username=z2
jdbc.password=123456
2.1 domain.java
package com.daydayhave.ssm.domain; public class Department {
private Long id;
private String name; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Department() {
} public Department(String name) { this.name = name;
}
}
2.2 DepartmentMapper.java
package com.daydayhave.ssm.mapper; import com.daydayhave.ssm.domain.Department; import java.util.List; public interface DepartmentMapper {
List<Department> loadAll();
void save(Department department);
}
2.3 DepartmentMapper.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.daydayhave.ssm.mapper.DepartmentMapper" >
<!-- List<Department> loadAll();
void save(Department department); -->
<select id="loadAll" resultType="Department">
SELECT * from t_department
</select>
<insert id="save" parameterType="Department">
INSERT INTO t_department(name) values(#{name})
</insert>
</mapper>
2.4 IDepartmentService.java
package com.daydayhave.ssm.service; import com.daydayhave.ssm.domain.Department; import java.util.List; public interface IDepartmentService {
List<Department> getAll();
void add(Department department);
}
2.5 DepartmentServiceImpl.java
package com.daydayhave.ssm.service.impl; import com.daydayhave.ssm.domain.Department;
import com.daydayhave.ssm.mapper.DepartmentMapper;
import com.daydayhave.ssm.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service
public class DepartmentServiceImpl implements IDepartmentService { @Autowired
private DepartmentMapper departmentMapper; @Override
public List<Department> getAll() {
return departmentMapper.loadAll();
} //写事务:需要一个事务,并且不是只读
//@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
@Transactional//默认就是写事务
@Override
public void add(Department department) {
departmentMapper.save(department);
//int i = 1/0;
}
}
3.1 baseTest.java
package com.daydayhave.ssm; import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BaseTest {
}
3.2 DepartmentServiceImplTest.java
package com.daydayhave.ssm.service.impl; import com.daydayhave.ssm.BaseTest;
import com.daydayhave.ssm.domain.Department;
import com.daydayhave.ssm.mapper.DepartmentMapper;
import com.daydayhave.ssm.service.IDepartmentService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import java.util.List; import static org.junit.Assert.*; public class DepartmentServiceImplTest extends BaseTest{ @Autowired
IDepartmentService departmentService; @Autowired
DepartmentMapper departmentMapper; @Test
public void getAll() {
List<Department> departments = departmentService.getAll();
System.out.println(departments);
} @Test
public void add() {
System.out.println(departmentMapper);
}
}
4.1 DepartmentController.java
package com.daydayhave.ssm.web.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/department")
public class DepartmentController { //跳转管理页面
@RequestMapping("/index")
public String index(){ return "department/index";
} }
maven_SSM集成的demo的更多相关文章
- dubbo环境搭建与tomcat集成、DEMO示例、常见问题(最完整版本、带管理控制台、监控中心、zookeeper)
以windows为例,linux基本相同,开发环境一般linux,个人环境一般windows(如果不开额外vm的话). 示例以dubbo官方自带demo为例子,进行整合和稍加修改测试. 0.dubbo ...
- XGPush集成(信鸽集成)demo
#import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #defi ...
- ActiveMQ与spring集成实现Queue模式
ActiveMQ可以和spring很好的集成,下面我们来看看,如何做个集成的demo. (1)pom.xml引入相关jar <!-- spring相关 begin --> <depe ...
- iOS集成ijkplayer视频直播框架,遇到的bug和坑...
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #555555 } p. ...
- 集成IJKplayer视频直播框架
ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持 Android 和 iOS, 网上也有很多集成说明, 但是个人觉得还是不够详细, 在这里详细的讲一下在 iOS 中如何集成ijk ...
- Dubbo集成Spring与Zookeeper实例
>>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...
- iOS:集成ijkplayer视频直播
介绍: ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持 Android 和 iOS, 网上也有很多集成说明, 但是个人觉得还是不够详细, 在这里详细的讲一下在 iOS 中如何集 ...
- android app 集成 支付宝支付 微信支付
项目中部分功能点需要用到支付功能,移动端主要集成支付宝支付和微信支付 支付宝sdk以及demo下载地址:https://doc.open.alipay.com/doc2/detail.htm?spm= ...
- iOS支付宝,微信,银联支付集成封装(上)
一.集成支付宝支付 支付宝集成官方教程https://docs.open.alipay.com/204/105295/ 支付宝集成官方demo https://docs.open.alipay.com ...
随机推荐
- RSA加密解密算法
/** * RSA加密解密算法 * Class Rsa */ class Rsa { /** * 获取pem格式的公钥 * @param $public_key 公钥文件路径或者字符串 * @retu ...
- 有名的素MM
from math import sqrt item=[] for yr in [1988,1989]: for mth in range(1,13): if mth in [1,3,5,7,8,10 ...
- 处理 oracle 数据库导入报错“IMP-00058: 遇到 ORACLE 错误 942”
在导入数据文件的时候出现了下图错误: 经过多次百度搜索问题.得知问题错误方向: 仔细的查询了被导入数据的数据库的版本: 而 被导入的数据包 dmp 文件是从 oracle11g r2的版本导出的. 所 ...
- python2 使用pip安装psycopg2出现错误:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-mvzdNj/psycopg2/
公司业务需求,开发语言python2,需要使用数据库:postgresql,需要安装模块psycopg2这个模块, 使用pip install psycopg2 报错: Command "p ...
- Day06(类包、内部类)
在static静态方法中能直接调用的方法只能是静态方法.要想调用其它非静态方法,需要借助对象. 类包:是Java提供的一种管理类文件的机制.可以解决类名冲突问题,在开发庞大应用程序时帮助开发人员管理庞 ...
- composer操作简单解析
1. composer配置中国镜像 #使用命令: composer config -e#修改composer.json 添加如下代码 { "repositories": [ { & ...
- 身份证号验证js程序
最近注册一个网站,居然让我输入个人身份证号,身份证号是个人信息,怎么能告诉你呢? 输入正确的身份证号没有任何问题. 我就仅仅改了最后一位,就告诉我身份证号不对,你是怎么知道的呢?所以,搜了下身份证号的 ...
- 并发编程futuretask
package com.mrbird.api.demoThread.thread1; /** * @Description * @Date: 2019/3/29 */ import java.util ...
- 关于jQuery的append方法不能多次添加同一个DOM元素的解决方法
资料来自:https://segmentfault.com/q/1010000007677851?_ea=1419689 append()方法在jQuery中是使用appendChild()实现的,实 ...
- Linux下crontab计划任务使用详解
Linux在相应用户下,用crontab -l 命令可以查看该用户定时执行的任务,-e可以编辑,但是其真实文件在哪儿呢??以CentOS为例,其真实的位置在:/var/spool/cron下面,有执行 ...