spring+springMVC+mybatis整合(1)
来源:互联网 发布:地图软件电脑版 编辑:程序博客网 时间:2024/05/20 08:41
今天整理一下ssm框架,下载文档点击打开链接
我的文档结构
前面的实体类和dao和mapper可以看我前面的文章逆向工程实现,废话少说先上代码
实体类
package cn.com.demo.entity;public class Demo { private Integer demoId; private String demoName; private String demoPassword; public Integer getDemoId() { return demoId; } public void setDemoId(Integer demoId) { this.demoId = demoId; } public String getDemoName() { return demoName; } public void setDemoName(String demoName) { this.demoName = demoName == null ? null : demoName.trim(); } public String getDemoPassword() { return demoPassword; } public void setDemoPassword(String demoPassword) { this.demoPassword = demoPassword == null ? null : demoPassword.trim(); }}
DAO
package cn.com.demo.dao;import cn.com.demo.entity.Demo;public interface DemoMapper { int deleteByPrimaryKey(Integer demoId); int insert(Demo record); int insertSelective(Demo record); Demo selectByPrimaryKey(Integer demoId); int updateByPrimaryKeySelective(Demo record); int updateByPrimaryKey(Demo record);}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="cn.com.demo.dao.DemoMapper" > <resultMap id="BaseResultMap" type="cn.com.demo.entity.Demo" > <id column="demo_id" property="demoId" jdbcType="INTEGER" /> <result column="demo_name" property="demoName" jdbcType="VARCHAR" /> <result column="demo_password" property="demoPassword" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > demo_id, demo_name, demo_password </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from demo where demo_id = #{demoId,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from demo where demo_id = #{demoId,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="cn.com.demo.entity.Demo" > insert into demo (demo_id, demo_name, demo_password ) values (#{demoId,jdbcType=INTEGER}, #{demoName,jdbcType=VARCHAR}, #{demoPassword,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="cn.com.demo.entity.Demo" keyProperty="demoId" useGeneratedKeys="true"> insert into demo <trim prefix="(" suffix=")" suffixOverrides="," > <if test="demoId != null" > demo_id, </if> <if test="demoName != null" > demo_name, </if> <if test="demoPassword != null" > demo_password, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="demoId != null" > #{demoId,jdbcType=INTEGER}, </if> <if test="demoName != null" > #{demoName,jdbcType=VARCHAR}, </if> <if test="demoPassword != null" > #{demoPassword,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="cn.com.demo.entity.Demo" > update demo <set > <if test="demoName != null" > demo_name = #{demoName,jdbcType=VARCHAR}, </if> <if test="demoPassword != null" > demo_password = #{demoPassword,jdbcType=VARCHAR}, </if> </set> where demo_id = #{demoId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="cn.com.demo.entity.Demo" > update demo set demo_name = #{demoName,jdbcType=VARCHAR}, demo_password = #{demoPassword,jdbcType=VARCHAR} where demo_id = #{demoId,jdbcType=INTEGER} </update></mapper>
service类
package cn.com.demo.service;import cn.com.demo.entity.Demo;public interface IDemoService{ int deleteByPrimaryKey(Integer demoId); int insert(Demo record); int insertSelective(Demo record); Demo selectByPrimaryKey(Integer demoId); int updateByPrimaryKeySelective(Demo record); int updateByPrimaryKey(Demo record);}
service实现类
package cn.com.demo.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import cn.com.demo.dao.DemoMapper;import cn.com.demo.entity.Demo;import cn.com.demo.service.IDemoService;@Servicepublic class DemoServiceImpl implements IDemoService{@Autowiredprivate DemoMapper demoMapper;@Overridepublic int deleteByPrimaryKey(Integer demoId){// TODO Auto-generated method stubreturn demoMapper.deleteByPrimaryKey(demoId);}@Overridepublic int insert(Demo record){// TODO Auto-generated method stubreturn demoMapper.insert(record);}@Overridepublic int insertSelective(Demo record){// TODO Auto-generated method stubreturn demoMapper.insertSelective(record);}@Overridepublic Demo selectByPrimaryKey(Integer demoId){// TODO Auto-generated method stubreturn demoMapper.selectByPrimaryKey(demoId);}@Overridepublic int updateByPrimaryKeySelective(Demo record){// TODO Auto-generated method stubreturn demoMapper.updateByPrimaryKeySelective(record);}@Overridepublic int updateByPrimaryKey(Demo record){// TODO Auto-generated method stubreturn demoMapper.updateByPrimaryKey(record);}}control类
package cn.com.demo.control;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import cn.com.demo.entity.Demo;import cn.com.demo.service.IDemoService;@Controller@RequestMapping("/test")public class DemoControl{@Autowiredprivate IDemoService demoService;@RequestMapping("/demo.html")public String demoTest(){Demo demo=new Demo();demo.setDemoName("username");demo.setDemoPassword("password");int id=demoService.insertSelective(demo);System.out.println(demo.getDemoId()+"qqqqqqqqqqqqqqqqq");return "demo";}}spring-context.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!--1. 引入配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list> <value>classpath:db.properties</value> </list> </property></bean><!--2. 数据源的创建 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean><!--3. spring和MyBatis完美整合,不需要mybatis的配置映射文件 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mapping.xml文件 --><property name="mapperLocations" value="classpath:cn/com/demo/mapper/*.xml"></property></bean><!-- 4.dao包下,spring会自动扫描以下 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.com.demo.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- 5.spring事务管理 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 6.面向切面 --><tx:advice id="tx" transaction-manager="transactionManager"><tx:attributes><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="find*" read-only="true"/><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice><aop:config><aop:pointcut expression="execution(* cn.com.demo.service.*.*(..))" id="ap"/><aop:advisor advice-ref="tx" pointcut-ref="ap"/></aop:config></beans>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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 注解标签驱动开启 --><mvc:annotation-driven></mvc:annotation-driven><!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源 --><mvc:default-servlet-handler /><context:annotation-config /><!-- 静态资源映射 --><mvc:resources location="/WEB-INF/static/**" mapping="/static/**"/><!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- --><bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="contentType" value="text/html" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!--避免IE执行AJAX时,返回JSON出现下载文件 --><bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /><!-- JSON转换器 --></list></property></bean><!-- 开启controller注解支持 --><!-- 注意事项请参考:http://jinnianshilongnian.iteye.com/blog/1762632 --><context:component-scan base-package="cn"></context:component-scan><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 最大下载速度 --><property name="maxUploadSize"><value>104857600</value></property><property name="maxInMemorySize"><value>2048</value></property></bean></beans>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>ssm</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-context.xml,classpath:spring/spring-mvc.xml</param-value> </context-param> <!-- 防止spring内存溢出 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springmvc控制器 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- springmvc控制器映射 --> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>将项目部署到tomcat上启动:
0 0
- [框架整合]spring+springmvc+mybatis(1)
- spring+springMVC+mybatis整合(1)
- Spring-SpringMVC-Mybatis整合
- springmvc+spring+mybatis整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- springmvc+mybatis+spring整合
- springmvc+spring+mybatis 整合
- mybatis+springmvc+spring 整合
- SpringMVC+Spring+Mybatis整合
- springmvc+spring+mybatis 整合
- springmvc+spring+mybatis整合
- Spring+Springmvc+MyBatis整合
- Spring+Springmvc+MyBatis整合
- Spring+Springmvc+MyBatis整合
- springmvc+mybatis+spring 整合
- springmvc+mybatis+spring 整合
- spring+springMVC+Mybatis整合
- python 库大全
- DataTables warning : Requested unknown parameter '1' from the data source for row 0
- Android TransformException:java.util.zip.ZipException
- iOS10下app运行中,但是处于后台,点击通知问题
- (管理后台3)添加识别图片
- spring+springMVC+mybatis整合(1)
- Oracle中date与timestamp的异同
- NIO/IO(二)
- C++“准”标准库Boost学习指南(8):Boost.Tuple
- windows cmd 查看md5 (结果16 进制)
- HTML练习--制作宣传视频
- android:focusableInTouchMode
- String s = new String(“xyz”);产生几个对象?
- 2017 ThoughtWorks校招编程作业(C++版)