web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Archetype Created Web Application</display-name> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 默认放在classpath下,就不需要这里指定 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:conf/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!--********* 编码格式过滤器 *********--> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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-3.2.xsd"> <context:annotation-config /> <!-- 扫描controller(controller层注入) --> <context:component-scan base-package="cn.springmvc.controller" /> <!-- 避免IE在ajax请求时,返回json出现下载 --> <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 对模型视图添加前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> </beans>
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="cn.springmvc" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/myoa?useUnicode=true&characterEncoding=UTF-8"> </property> <property name="username" value="root"></property> <property name="password" value="system"></property> </bean> <!-- mybatis文件配置,扫描所有mapper文件 configLocation为mybatis属性 mapperLocations为所有mapper--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:conf/mybatis-config.xml" p:mapperLocations="classpath:mapper/*.xml" /> <!-- spring与mybatis整合配置,扫描所有dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="cn.springmvc.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" /> <!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> </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> <typeAlias alias="User" type="cn.springmvc.model.User"/> </typeAliases> <!-- 映射map --> <mappers> </mappers> </configuration>
log4j.properties
log4j.rootLogger = debug , stdout , D log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = c:/error.log log4j.appender.D.Append = true log4j.appender.D.Threshold = ERROR log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
userMapper.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="cn.springmvc.dao.UserDAO"> <insert id="insertUser" parameterType="User" keyProperty="id"> insert into days_user( state, nickname) values ( #{state}, #{nickname}) </insert> </mapper>
package cn.springmvc.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.springmvc.model.User; import cn.springmvc.service.UserService; public class UserTest { private UserService userService; @Before public void before() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:applicationContext.xml", "classpath:mybatis-config.xml" }); userService = (UserService) context.getBean("userService"); } @Test public void addUser() { User user = new User(); user.setNickname("你好"); user.setState(2); System.out.println(userService.insertUser(user)); } }
package cn.springmvc.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.springmvc.dao.UserDAO; import cn.springmvc.model.User; @Service public class UserService { @Autowired private UserDAO userDAO; public int insertUser(User user) { return userDAO.insertUser(user); } }
package cn.springmvc.model; /** * 用户表 */ public class User { private int id; private int state; private String nickname; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } }
package cn.springmvc.dao; import cn.springmvc.model.User; public interface UserDAO { /** * 添加新用户 * @param user * @return */ public int insertUser(User user); }
package cn.springmvc.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import cn.springmvc.model.User; import cn.springmvc.service.UserService; @Controller @RequestMapping("/test.do") public class UserController { @Autowired private UserService userService; @RequestMapping(params = "method=login") public String index( HttpServletRequest request) { User user = new User(); user.setNickname("gg"); user.setState(2); System.out.println(userService.insertUser(user)); return "index"; } }
<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>jms</groupId> <artifactId>jms</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>jms</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.4.RELEASE</spring.version> <mybatis.version>3.2.4</mybatis.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.9</log4j.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <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> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.5</version> </dependency> </dependencies> <build> <finalName>jms</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <webXml>webapp\WEB-INF\web.xml</webXml> </configuration> </plugin> </plugins> </build> </project>
相关推荐
基于springboot2+mybatis+thymeleaf整合开发的在线心理咨询问卷管理系统 项目描述 spring boot2开发的在线心理咨询问卷管理系统 运行环境 jdk8+mysql5.7+IntelliJ IDEA+maven 项目技术 spring boot2+mybatis+...
在MyBatis与Spring的初步整合过程中,主要涉及以下几个关键点: 1. **引入依赖**:首先,在项目中引入MyBatis和Spring的相关库,如mybatis-spring、mybatis、spring-jdbc和spring-core等。 2. **配置Spring**:在...
【Spring MVC 入门教程:整合 Mybatis 与 Oracle 实现简单查询】 在这个教程中,我们将探讨...通过这个入门实例,你可以对 Spring MVC、Mybatis 和 Oracle 的整合有一个初步了解,为进一步深入学习和实践打下基础。
Yan项目简介(使用说明)注:全新微服务项目lion已经逐步开发,基于SpringCloud的一整套方案,详情请查看本项目是一整合整合Dubbo + Zookeeper + SpringMVC + Spring + MyBatis支持分布式的高效率快速开发RPC框架,...
基于IDEA+MySQL实现SSM初步整合 1. 数据库版本: mysql8.0.26 2. IDEA版本: idea2020 3. JDK版本: jdk1.8.1 4. Tomcat版本: apache-tomcat-9.0.56 5. Maven版本: maven3.8.1 6. MyBatis版本: mybaits3.5.9 7. ...
mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...
- 第十六天资料:可能深入到了SSM整合的实践,如何在Spring中配置SpringMVC和MyBatis,以及事务管理。 每个阶段的学习资料都可能包含实战项目、代码示例和问题解析,帮助读者逐步掌握SSM框架的精髓。通过这样的学习...
这个小demo是SSM框架整合的一个初步示例,虽然AOP(面向切面编程)部分未完成,但依然可以为我们展示SSM的核心概念和基本配置。 1. **Spring框架**: - **依赖注入(DI)**:Spring的核心特性,通过XML配置或注解...
SSM整合是Java Web开发中常见的一种框架组合,包括Spring、SpringMVC和Mybatis。这个压缩包提供了SSM整合所需的全部jar包,版本分别为Spring 4.3和Mybatis 3.4,这两个版本都是相对较为现代且稳定的,确保了良好的...
如何实现spring3.0.5与mybatis3.0.5的整合。 如何实现spring3.0.5与Stripes1.5.6(类似Struts框架)的整合。 如何使用mybatis3.0.5框架。 压缩包说明: 文件解压后有mybatis(可直接导入eclipse运行的项目)和mybatis-...
在整合Mybatis时,SpringMVC可以管理SqlSession和Mapper接口,提供事务控制,简化数据访问层的代码。 MongoDB是一个文档型数据库,支持JSON格式的数据存储。它的灵活性和高伸缩性使其在大数据和分布式系统中很受...
例如,实验1可能是Spring框架的初步接触,实验5可能涉及Spring MVC的控制器设计,而实验12可能涵盖MyBatis的高级特性,如动态SQL。实验报告将详细记录每个步骤,帮助你巩固理解并解决可能出现的问题。 每个实验都是...
4. 配置数据源,连接Oracle数据库,并整合Mybatis和Spring的事务管理。 5. 创建Model、View和Controller等MVC组件,编写业务逻辑。 ### 总结 整合SpringMVC和Mybatis的Maven项目,需要先搭建好开发环境,之后创建...
《SpringBoot+SpringMVC+MyBatis+MySQL:企业级项目整合详解》 在现代企业级应用开发中,...通过学习和实践这个项目,不仅可以掌握四大技术的整合运用,也能对微服务架构有初步的理解,进一步提升自身的开发技能。
此外,还会涉及到MyBatis的缓存机制、插件扩展、Spring与MyBatis的整合等进阶内容。对于初学者,理解MyBatis的核心思想和基本用法是首要任务,随着经验的积累,可以深入研究其内部原理和高级特性。
在本文中,我们将深入探讨如何在Myeclipse和IntelliJ IDEA(简称idea)这两个流行的IDE中进行SSM框架的初步整合。首先,我们需要理解这三个组件的基本概念。 **Spring框架** 是一个全面的后端应用框架,提供了依赖...
9. **配置Spring**(可选):为了实现更高级的依赖注入和事务管理,可以将MyBatis与Spring框架整合。在Spring的配置文件中,添加MyBatis的SqlSessionFactoryBean和MapperScannerConfigurer。 10. **编写测试代码**...
通过整合SpringMVC+SpringBoot+Mybatis构建一个可用的基于SpringCloud的微服务工程将上述技术组件逐步落地,让大家看得懂做得出学得会。 00、硅谷学习_SpringBoot_源码、课件 01.硅谷学习_SpringCloud_前提概述 ...
MyBatis-Plus-示例Spring Boot整合MyBatis Plus实例,持续更新!!!代码生成器三方框架版本总览名称版本更新时间Sprint Boot2.4.2 2021.02.02 MyBatis-Plus 3.4.2 2021.02.02 Java库3.4.2 2021.02.02 api结果2.3.1 ...