Spring3MVC+Mybatis快速使用
来源:互联网 发布:java线程源码 编辑:程序博客网 时间:2024/06/08 10:49
1.项目结构:
2.代码:
1.User.xml Article.java User.javaI UserOperation.java:参考mybatis快速使用
2.UserController.java:请求处理
package com.spring3.controller;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.mybatis.bean.Article;import com.mybatis.service.IUserOperation;@Controller@RequestMapping("/article")public class UserController {@Autowired//类型注入IUserOperation userMapper;@RequestMapping("/list")public ModelAndView listall(HttpServletRequest requset,HttpServletResponse response){List<Article> articles = userMapper.getUserArticles(1);ModelAndView mav = new ModelAndView("list");mav.addObject("articles", articles);return mav;}}
3.Configuration.xml:可以不再导入mapper
<?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="com.mybatis.bean.User"/><typeAlias type="com.mybatis.bean.Article"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><!-- <mappers><mapper resource="com/mybatis/bean/User.xml"/></mappers> --></configuration>
4.applicationContext.xml:配置数据源、sqlsessionfactory、事务、实例化接口类mapper bean
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 加载数据库配置文件 --> <!-- <context:property-placeholder location="classpath:/config/database.properties"/> --> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql:///mybatis?characterEncoding=utf8" p:username="root" p:password="root" p:maxActive="20" p:maxIdle="5"> </bean> <!-- 注册事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--注册spring管理sessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 指定factory配置文件 --> <property name="configLocation" value="classpath:config/Configuration.xml"></property> <!-- 导入所有mapper配置文件 --> <property name="mapperLocations" value="classpath*:com/mybatis/bean/*.xml"></property> </bean> <!-- 通过扫描包,实例化所有的接口bean,这是一个bean集合 扫描实例化MapperScannerConfigurer 实例化调用MapperFactoryBean 注入是可使用@Autowired 类型装配--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包 使用分号或逗号 作为分隔符设置多于一个的包路径--> <property name="basePackage" value="com.mybatis.service"></property> </bean> </beans>
5.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring3Mybatis</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*:config/applicationContext.xml</param-value> </context-param> <!-- 启动监听加载spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 注册servlet,加载dispatcher配置文件 --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <!-- 默认加载mvc-dispatcher-servlet.xml 的servlet配置 --> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6.mvc-dispatcher-servlet.xml:驱动注解、一些全局配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 包扫描,spring解析驱动 --> <context:component-scan base-package="com.spring3.controller"></context:component-scan> <!-- mvc注解驱动 --> <mvc:annotation-driven/> <!-- 静态资源控制 --> <mvc:resources location="/WEB-INF/static" mapping="/static/**"/> <mvc:default-servlet-handler/> <!-- 注册请求视图处理器 前后缀配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean></beans>
7.测试:
<body><c:forEach items="${articles }" var="item">${itme.id}--${item.title }--${item.content }<br/></c:forEach></body>
3.总结:
3.Configuration.xml:可以不再导入mapper
<?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="com.mybatis.bean.User"/><typeAlias type="com.mybatis.bean.Article"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><!-- <mappers><mapper resource="com/mybatis/bean/User.xml"/></mappers> --></configuration>
4.applicationContext.xml:配置数据源、sqlsessionfactory、事务、实例化接口类mapper bean
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 加载数据库配置文件 --> <!-- <context:property-placeholder location="classpath:/config/database.properties"/> --> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql:///mybatis?characterEncoding=utf8" p:username="root" p:password="root" p:maxActive="20" p:maxIdle="5"> </bean> <!-- 注册事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--注册spring管理sessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 指定factory配置文件 --> <property name="configLocation" value="classpath:config/Configuration.xml"></property> <!-- 导入所有mapper配置文件 --> <property name="mapperLocations" value="classpath*:com/mybatis/bean/*.xml"></property> </bean> <!-- 通过扫描包,实例化所有的接口bean,这是一个bean集合 扫描实例化MapperScannerConfigurer 实例化调用MapperFactoryBean 注入是可使用@Autowired 类型装配--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包 使用分号或逗号 作为分隔符设置多于一个的包路径--> <property name="basePackage" value="com.mybatis.service"></property> </bean> </beans>
5.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring3Mybatis</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*:config/applicationContext.xml</param-value> </context-param> <!-- 启动监听加载spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 注册servlet,加载dispatcher配置文件 --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <!-- 默认加载mvc-dispatcher-servlet.xml 的servlet配置 --> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6.mvc-dispatcher-servlet.xml:驱动注解、一些全局配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 包扫描,spring解析驱动 --> <context:component-scan base-package="com.spring3.controller"></context:component-scan> <!-- mvc注解驱动 --> <mvc:annotation-driven/> <!-- 静态资源控制 --> <mvc:resources location="/WEB-INF/static" mapping="/static/**"/> <mvc:default-servlet-handler/> <!-- 注册请求视图处理器 前后缀配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean></beans>
7.测试:
<body><c:forEach items="${articles }" var="item">${itme.id}--${item.title }--${item.content }<br/></c:forEach></body>
3.总结:
<body><c:forEach items="${articles }" var="item">${itme.id}--${item.title }--${item.content }<br/></c:forEach></body>
1.configuration.xml文件中的数据库配置可以不用
2.其他配置和spring+mybatis一样,唯有spring配置文件在 实例化mapper时配置略有不同,不过都是通过MapperFactoryBean去实例化接口mapper。
@Autowired类型注入
0 0
- Spring3MVC+Mybatis快速使用
- spring3mvc 整合 mybatis
- spring3mvc+mybatis 环境搭建
- mybatis快速使用
- mybatis+spring快速使用
- 快速学会使用MyBatis
- [springMVC]Spring3MVC 在JSP中使用@ModelAttribute
- Spring3MVC 在JSP中使用@ModelAttribute
- [springMVC]Spring3MVC 在JSP中使用@ModelAttribute
- Spring3MVC+MyBatis+ExtJs3整合开发系列之一:登录模块演示
- Spring3mvc,在使用ajax时发生的问题,求解?
- spring3mvc中使用ajax传json中文乱码解决
- spring3mvc中使用ajax传json中文乱码解决
- Spring3MVC 在JSP中使用@ModelAttribute--源自技术
- Spring3MVC通过SpringContextUtil获取上下文(与不使用对比)
- 如何快速学习使用mybatis以及总结
- 使用mybatis-generator-core快速生成代码
- 使用mybatis-generator来快速生成代码
- springMVC 400 错误总结
- bdd1.3.2 install
- ArrayList,LinkedList, Vector三者的区别
- redis源码学习之跳跃表
- php微信支付开发实例
- Spring3MVC+Mybatis快速使用
- String, StringBuffer, StringBuilder三者区别
- c#-ADO.NET链接数据库
- Centos7上Docker仓库Harbor的搭建
- 求三角形面积-gyy
- 线程同步的方法
- 线段树入门
- java单例模式的实现方式
- java提高篇(二三)-----HashMap