代码目录:

/Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC

1. 首先在resource目录加上jdbc.properties:

driverClasss=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://10.117.x.x:8306/springdemo?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=x
password=x #定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

2. 在pom.xml里面添加如下依赖:

<properties>
<spring.version>4.2.6.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mysql-driver.version>5.1.29</mysql-driver.version>
</properties> <!-- 添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency> <!-- 添加mybatis/spring整合包依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency> <!-- 添加mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version>
</dependency>
<!-- 添加数据库连接池依赖 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>

3. 把之前的applicationContext.xml配置 refactor成 spring-mybatis.xml,用refactor菜单的时候,web.xml里面如下也会改:

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mybatis.xml</param-value>
</context-param>

4. 最终写成的spring-mybatis.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/cache"
xmlns:contex="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/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <contex:component-scan base-package="com.webapp.hello"/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClasss}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxWait" value="${maxWait}"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" value="dataSource"/>
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.webapp.hello.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> </beans>

5. 几个代码文件如下:

HelloController

package com.webapp.hello;

import com.webapp.hello.dao.UserDao;
import com.webapp.hello.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List; /**
* Created by baidu on 16/10/7.
*/
@Controller
public class HelloController { @Resource
UserDao userDao; @RequestMapping(value="/", method= RequestMethod.GET)
public String index(ModelMap modelMap) {
/*List userList = new LinkedList();
userList.add("张三");
userList.add("李四");
modelMap.addAttribute("userList", userList);*/ List<User> userList = userDao.selectAllUser();
modelMap.addAttribute("userList", userList);
return "hello";
}
}

model/User

package com.webapp.hello.model;

/**
* Created by baidu on 16/10/8.
*/
public class User {
private Long id;
private String userName; public String getUserName() {
return userName;
} }

dao/UserDao

package com.webapp.hello.dao;

import com.webapp.hello.model.User;
import org.springframework.stereotype.Repository; import java.util.List; /**
* Created by baidu on 16/10/8.
*/
@Repository
public interface UserDao {
List<User> selectAllUser();
}

mapping/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="com.webapp.hello.dao.UserDao">
<resultMap id="UserMap" type="com.webapp.hello.model.User">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="user_name" property="userName" jdbcType="VARCHAR"/>
</resultMap> <select id="selectAllUser" resultMap="UserMap">
SELECT id, user_name FROM t_user
</select> </mapper>

Jsp文件,对user加上属性:

${user.userName}

运行之后,首先报的一个错,是针对sqlSessionFactory初始话,不能convert string to DataSource。对比排查后,在spring-mybatis.xml里面改了下面第二行的value变成 ref:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
</bean>

然后再运行,报错找不到class,是commons/pool等,应该就是commons-pool的依赖,所以在pom.xml加上对commons-pool的依赖:

<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>

然后再运行,就可以成功显示了:

在上已个Java Spring MVC项目基础上加MyBatis的更多相关文章

  1. Java Spring MVC项目搭建(二)——项目配置

    1.站点配置文件web.xml 每一个Spring MVC 项目都必须有一个站点配置文件web.xml,他的主要功能吗....有一位大哥已经整理的很好,我借来了,大家看看: 引用博客地址: http: ...

  2. Java Spring MVC项目搭建(一)——Spring MVC框架集成

    1.Java JDK及Tomcat安装 我这里安装的是JDK 1.8 及 Tomcat 8,安装步骤详见:http://www.cnblogs.com/eczhou/p/6285248.html 2. ...

  3. 自建一个Java Spring MVC项目

    用IDEA Intellij,本来创建的是SpringMVC项目,但是下载的时候,太慢了.所以还是用的Maven项目. 选择Maven 项目->Archetype->Web applica ...

  4. Java Spring MVC项目搭建(三)——“Hello World”

    在Spring 的配置文件里,我们定义了一个bean ,Spring 会在启动时候会生成对象. <bean id = "helloworld" class="com ...

  5. java spring boot项目部署-上

    1.编写sh脚本,便于服务器上管理工程: #!/bin/bash source /etc/profile PROG_NAME=$ ACTION=$ usage() { echo "Usage ...

  6. IntelliJ IDEA上创建maven Spring MVC项目

    IntelliJ IDEA上创建Maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...

  7. Java Web 学习(8) —— Spring MVC 之文件上传与下载

    Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...

  8. 【Spring】搭建最简单的Spring MVC项目

    每次需要Spring MVC的web项目测试一些东西时,都苦于手头上没有最简单的Spring MVC的web项目,现写一个. > 版本说明 首先要引入一些包,Spring的IOC.MVC包就不用 ...

  9. maven Spring MVC项目

    IntelliJ IDEA上创建maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...

随机推荐

  1. 谷歌翻译python接口

    项目地址:  https://github.com/ssut/py-googletrans 安装: sudo pip install googletrans 使用: #!/usr/bin/python ...

  2. Typo: In word 拼写检查

    Settings->Inspections > Spelling > Typo 评写检查,

  3. 【ASP.NET MVC】Scripts目录

    很多时候我们经常在用的东西我们可能不一定真正的了解,因为我们可能已经会用了,便不再对其进行探索,下面我们看一下在ASP.NET MVC3项目下的Scripts目录下的文件: Jquery核心库我们就不 ...

  4. elementUI 学习入门之 container 布局容器

    Container 布局容器 用于布局的容器组件,方便快速搭建页面基本结构 <el-container> : 外层容器.当子元素包含 <el-header> 或 <el- ...

  5. django 编码错误

    估计这个问题是2.7的问题3.0好像就统一utf编码了 报错代码: python :ascii codec can't decode byte 0xe8 in posi 当django中报这个错误的时 ...

  6. Sea Battle<海战>(思路题)

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Cpp下的深拷贝与浅拷贝探究

    下面,通过代码来说说C++中的深浅拷贝 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; cl ...

  8. 51Nod1802 左偏树计数

    题目大意 求$n$个点的无标号左偏树个数 既然你都点进来了,那么估计也是奔着题解来的.... 废话少说.... 首先,左偏树有这么一些性质 设最右链长度为$r[p]$ 1.左偏树的子树仍然是左偏树 2 ...

  9. bzoj 3165

    题意: 给出平面上一些线段,在线询问与x=x0相交的线段中,交点y最大的线段的标号,支持添加线段. 大概思路: 用线段树维护,线段树每个线段记录贯穿(左右端点在该区间外或上)的原线段中能覆盖其它贯穿该 ...

  10. Unity UGUI之Text

    下图是Text组件的内容. Character(字符) Text--输入要显示的文本 Font--要渲染文本的字体类型(例如:黑体.宋体) FontStyle--是否要加粗,倾斜等. Normal-- ...