maven_SSM集成的demo
Posted daydayhave
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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注解支持[email protected]--> <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的主要内容,如果未能解决你的问题,请参考以下文章
[vscode]--HTML代码片段(基础版,reactvuejquery)
12mmaction2 行为识别商用级别X3D复现 demo实现 检测自己的视频 Expanding Architecturesfor Efficient Video Recognition(代码片段
[异常解决] Keil安装好nRF51822开发环境,运行DEMO报错:Error:“GPIOTE_CONFIG_NUM_OF_LOW_POWER_ENVENTS” is undefined(代码片段