Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

时间:2022-12-18 19:15:13

一些重要的知识:

mybais-spring.jar及其提供的API:

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

SqlSessionFactoryBean:

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

SqlSessionFactory是由SqlSessionFactoryBuilder产生的,
Spring整合MyBats时SqlSessionFactoryBean也是由SqlSessionFactoryBuilder生成的。

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

MapperFactoryBean:

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

在使用MapperFactoryBean时,有一个Mapper,就需要一个MapperFactoryBean。 
为此,需要基于扫描机制的,MapperScannerConfigurer。具体配置方法略。
只需配置要扫描的包。
将扫描该包下所有的带有@MyBatisRepository的Mapper。

第一阶段,spring整合mybatis

项目目录:

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
"
default-lazy-init="true"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql:///test"/>
<property name = "username" value = "root"/>
<property name = "password" value = "1234"/>
</bean> <bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref = "myDataSource"/>
<property name = "mapperLocations" value = "classpath:com/rixiang/entity/*.xml"/>
</bean> <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref = "sqlSessionFactory"/>
<property name="basePackage" value = "com.rixiang"/>
<property name = "annotationClass" value = "com.rixiang.annotation.MyBatisRepository"/>
</bean> </beans>

EmpDAO,记得添加@MybatisRepository注解:

package com.rixiang.dao;

import java.util.List;

import com.rixiang.annotation.MyBatisRepository;
import com.rixiang.entity.Emp; @MyBatisRepository
public interface EmpDAO {
public List<Emp> findAll();
}
MyBatisRepository:
package com.rixiang.annotation;

import org.springframework.stereotype.Repository;

/**
* Created by samdi on 2016/3/3.
*/
@Repository
public @interface MyBatisRepository {
String value() default "";
}

test:

package com.rixiang.test;

import com.rixiang.dao.EmpDAO;
import com.rixiang.entity.Emp;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException;
import java.util.List; /**
* Created by samdi on 2016/3/3.
*/
public class TestEmpDAO {
@Test
public void testFindAll() throws IOException {
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
EmpDAO mapper = ac.getBean("empDAO",EmpDAO.class);
List<Emp> list = mapper.findAll();
for(Emp emp:list){
System.out.println(emp.getEmpno() + " " + emp.getEname());
} }
}

第二阶段:SpringMVC+MyBatis:

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

controller:

package com.rixiang.web;

import java.util.List;

import com.rixiang.dao.EmpDAO;
import com.rixiang.entity.Emp; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("emp")
public class EmpListController {
private EmpDAO dao;
@Autowired
public void setDao(EmpDAO dao){
this.dao = dao;
}
@RequestMapping("/list")
public String execute(Model model){
List<Emp> list = dao.findAll();
model.addAttribute("emps",list);
return "emp_list";
}
}

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
"
default-lazy-init="true"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql:///test"/>
<property name = "username" value = "root"/>
<property name = "password" value = "1234"/>
</bean> <bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref = "myDataSource"/>
<property name = "mapperLocations" value = "classpath:com/rixiang/entity/*.xml"/>
</bean> <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref = "sqlSessionFactory"/>
<property name="basePackage" value = "com.rixiang"/>
<property name = "annotationClass" value = "com.rixiang.annotation.MyBatisRepository"/>
</bean> <context:component-scan base-package="com.rixiang"/> <!-- 支持@RequestMapping请求和Controller映射 -->
<mvc:annotation-driven/> <!-- 定义视图解析器viewResolver -->
<bean id = "viewResolver"
class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/"/>
<property name = "suffix" value = ".jsp"/>
</bean> </beans>

jsp:

<%@ page language = "java" import = "java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>员工列表示例</title>
</head> <body>
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>工资</td>
<td>入职时间</td>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.empno}</td>
<td>${emp.ename}</td>
<td>${emp.sal}</td>
<td>${emp.hiredate}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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:applicationContext.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

运行:

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

Spring MVC + MyBatis整合(IntelliJ IDEA环境下)的更多相关文章

  1. spring&comma; spring mvc&comma; mybatis整合文件配置详解

    转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...

  2. ssm整合说明与模板-Spring Spring MVC Mybatis整合开发

    ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...

  3. Spring &plus; Spring MVC &plus; MyBatis 整合

    1.所需要Jar包 ? <!-- Spring3.0.1包 -->   org.springframework.web-3.0.1 系列   <!-- 公共包 -->   sl ...

  4. Spring&plus;MVC&plus;Mybatis整合

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 什么是秒杀业务 网站售卖某产品时,规定在某个日期开始售卖限量的产品, ...

  5. Spring&plus;Spring MVC&plus;MyBatis整合

    一.准备工作    1.1导入所需jar包 1.2数据库 CREATE TABLE `t_customer` ( `id` ) NOT NULL AUTO_INCREMENT, `username` ...

  6. Spring4&plus;Spring MVC&plus;MyBatis整合思路

    1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...

  7. Spring &plus; Spring MVC &plus; MyBatis框架整合

    ---恢复内容开始--- 一.Maven Web项目创建 如有需要,请参考:使用maven创建web项目 二.Spring + Spring MVC + MyBatis整合 1.Maven引入需要的J ...

  8. Spring MVC 学习总结(十)——Spring&plus;Spring MVC&plus;MyBatis框架集成(IntelliJ IDEA SSM集成)

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  9. 基于Maven的Spring &plus; Spring MVC &plus; Mybatis的环境搭建

    基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...

随机推荐

  1. Ionic开发中常见问题和解决方案记录

    1npm按装包失败 更换源:npm config set registry https://registry.npm.taobao.org 或者使用cnpm sudo npm install -g c ...

  2. SQLite CRUD操作

    SQLite CRUD操作代码实例: 1:首先创建一个继承了SQLiteOpenHelper类的MyDatabaseHelper类.实现他的onCreate(SQLiteDatabase db) on ...

  3. hdoj 5194 DZY Loves Balls【规律&amp&semi;&amp&semi;gcd】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5194 题意:给你n个黑球,m个白球,每次从中随机抽取一个,如果抽到黑球记为1如果抽出来白球记为0,让你 ...

  4. poj3624 简单的01背包问题

    问题描述: 总共有N种宝石供挑选,宝石i的重量为Wi,吸引力为Di,只可以用一次.Bessie最多可负担的宝石手镯总重量为M.给出N,M,Wi,Di,求M. 非常标准的01背包问题.使用了优化的一维数 ...

  5. electron通讯

    Electron桌面应用实现两个窗口(渲染进程)之间的通讯,传输数据: 方法1:在两个网页(渲染进程)间共享数据最简单的方法是使用浏览器中已经实现的 HTML5 API. 其中比较好的方案是用 Sto ...

  6. Elasticsearch源码分析 - 源码构建

    原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483694&idx=1&sn=bd03afe5a ...

  7. Java多线程之volatile关键字《一》

    关键字volatile的主要作用是使变量在多个线程间可见. 1.关键字volatile与死循环 如果不是在多继承的情况下,使用继承Thread类和实现Runnable接口在取得程序运行的结果上并没有什 ...

  8. CodeBlocks中我遇到的无法调试问题及解决方案

    CodeBlocks中遇到无法调试问题,可能有很多种,以下是我遇到的问题及解决方案 1.当无法调试时,如果IDE提示你出现下图中红色问题,说明你的调试器没有装好 2.在菜单栏点击Settings--D ...

  9. &lpar;1&rpar;selenium-java环境搭建

    已经学过了用python模拟浏览器操作,现在开始尝试使用java搭建环境,开头第一步就遇到了很多的问题 1.准备jdk安装,不再描述,自行百度 2.安装eclipse 3.接下来就是新建项目了,new ...

  10. ubuntu 14&period;04 安装jdk 1&period;8

    一,如何删除低版本的open JDK? 在ubuntn的软件中心中,如果输入"Java",我们会看到open JDK,但是最高版本是1.7,也有1.6版本的,如果我们安装上去,可能 ...