设为首页 收藏本站
查看: 926|回复: 0

[经验分享] myBatis系列之六:与SpringMVC集成

[复制链接]

尚未签到

发表于 2016-11-26 06:05:36 | 显示全部楼层 |阅读模式
myBatis系列之一:搭建开发环境
myBatis系列之二:以接口方式交互数据
myBatis系列之三:增删改查
myBatis系列之四:关联数据的查询
myBatis系列之五:与Spring3集成
myBatis系列之七:事务管理

在myBatis系列之五:与Spring3集成基础上:
1. 往pom.xml添加SpringMVC和Freemarker依赖:

<properties>
<freemarker.version>2.3.19</freemarker.version>
<servlet.version>2.5</servlet.version>
</properties>
<dependency>  
<groupId>org.freemarker</groupId>  
<artifactId>freemarker</artifactId>  
<version>${freemarker.version}</version>  
</dependency>
<dependency>
<groupId>javax.servlet</groupId>  
<artifactId>servlet-api</artifactId>  
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>


2. 在web.xml中加入Spring的监听器和SpringMVC的servlet:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 监听容器事件,初始化和关闭Web应用上下文并调用ContextCleanupListener清理资源 -->
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class><!-- Web应用关闭时,清理ServletContext中spring相关的可销毁资源 -->
</listener>
<servlet>
<servlet-name>hbatis</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/hbatis-servlet.xml</param-value>
</init-param>--><!-- 未配置时,SpringMVC会到WEB-INF目录下找${project.name}-servlet.xml -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hbatis</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>


3. 在WEB-INF下新建:
Spring配置文件applicationContext.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"
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">
<context:property-placeholder location="classpath:/database.properties" /><!-- 数据库配置文件 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${driverClassName}"
p:url="${url}"
p:username="${user_name}"
p:password="${password}" /><!-- 数据源配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- sqlSessionFactory对象 -->
<property name="dataSource" ref="dataSource" /><!-- 数据源 -->
<property name="configLocation" value="classpath:Configuration.xml" /><!-- myBatis配置文件 -->
<!--<property name="mapperLocations" value="classpath*:com/john/hbatis/model/*.xml" />--><!-- 可以在Configuration.xml或此处配置映射文件,但其中不能有相同id的parameterMap, resultMap或sql等 -->
</bean>
<bean id="mapperConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描指定包以获取映射器 -->
<property name="basePackage" value="com.john.hbatis.mapper" />
</bean>
</beans>

类路径下的database.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
user_name=root
password=123456


注:因为MapperScannerConfigurer可能会导致username取的是系统用户的账号,而造成数据库连接失败,所以改成其它值:user_name。
SpringMVC配置文件hbatis-servlet.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">
<mvc:annotation-driven /><!-- 注册RequestMappingHandlerMapping, RequestMappingHandlerAdapter和ExceptionHandlerExceptionResolver以提供对@RequestMapping,@ExceptionHandler等注解的支持 -->
<context:component-scan base-package="com.john.hbatis.controller" /><!-- 扫描控制器包下有特定注解的类,并实例化和依赖注入 -->
<!-- FreeMarker视图处理器 -->
<bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>  
<property name="contentType" value="text/html;charset=utf-8"/>  
<property name="prefix" value="" />  
<property name="cache" value="false"/>  
<property name="viewNames">  
<array>  
<value>*.ftl</value>  
</array>  
</property>  
<!--<property name="suffix" value=".ftl"/>-->  
<property name="order" value="0"/><!-- 优先级,数值越小优先级越高 -->  
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
<property name="templateLoaderPaths">  
<list>  
<value>/WEB-INF/ftl/</value><!-- 模板加载路径 -->  
</list>  
</property>  
</bean>
</beans>


4. MVC:
控制层:UserController.java

@Controller
@RequestMapping("/article")
public class UserController {
@Autowired
IUserMapper mapper;
@RequestMapping("/list")
public String showAll(ModelMap modelMap) {
List<Article> articles = mapper.getArticlesByUserId(1);
modelMap.addAttribute("articles", articles);
return "main.ftl";
}
}


视图层:main.ftl:

<#list articles as article>
<div>${article.id}. ${article.title}: ${article.content}</div>
</#list>


5. 启动工程,浏览器输入:http://localhost:8080/hbatis/article/list.htm查看结果。
参考:
http://www.yihaomen.com/article/java/318.htm

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305544-1-1.html 上篇帖子: 整合spring与mybatis及相关配置 下篇帖子: MyBatis 实现简单的 增加 查找
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表