1 数据库准备
(1)建立一个名为mytest的数据库
(2)创建表
- CREATE TABLE `t_user` (
- `USER_ID` int(11) NOT NULL AUTO_INCREMENT,
- `USER_NAME` char(30) NOT NULL,
- `USER_PASSWORD` char(10) NOT NULL,
- PRIMARY KEY (`USER_ID`),
- KEY `IDX_NAME` (`USER_NAME`)
- )ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
(3)插入数据
- INSERT INTO t_user (USER_ID, USER_NAME,USER_PASSWORD) VALUES (1, ‘Zhang San‘, ‘654321‘);
- INSERT INTO t_user (USER_ID, USER_NAME,USER_PASSWORD) VALUES (2, ‘Li Si‘, ‘123456‘);
2 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为maven_springmvc_mybatis_demo的工程。
3 文件目录结构如下图所示
4 pom.xml的完整内容为
5 web.xml中的完整内容为
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
- <display-name>maven_springmvc_mybatis_demo</display-name>
-
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:application.xml</param-value>
- </context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
- <servlet>
- <servlet-name>dispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
-
- <param-value>classpath:springmvc/spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
6 User.Java的完整代码为
- package com.zheng.model;
-
- public class User {
- private Integer userId;
- private String userName;
- private String userPassword;
-
- public Integer getUserId() {
- return userId;
- }
-
- public void setUserId(Integer userId) {
- this.userId = userId;
- }
-
- public String getUserName() {
- return userName;
- }
-
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
- public String getUserPassword() {
- return userPassword;
- }
-
- public void setUserPassword(String userPassword) {
- this.userPassword = userPassword;
- }
- }
7 UserDao.java的完整代码为
- package com.zheng.dao;
-
- import com.zheng.model.User;
-
- public interface UserDao {
- public User selectUserById(Integer userId);
- }
8 UserService.java的完整代码为
- package com.zheng.service;
-
- import com.zheng.model.User;
-
- public interface UserService {
- User selectUserById(Integer userId);
- }
9 UserServiceImpl.java的完整代码为
- package com.zheng.service;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import com.zheng.dao.UserDao;
- import com.zheng.model.User;
- import com.zheng.service.UserService;
-
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
-
- public User selectUserById(Integer userId) {
- return userDao.selectUserById(userId);
-
- }
- }
10 UserController.java的完整代码为
- package com.zheng.controller;
-
- import javax.annotation.Resource;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- import com.zheng.model.User;
- import com.zheng.service.UserService;
-
- @Controller
- public class UserController {
-
- @Resource
- private UserService userService;
-
- @RequestMapping("/showUserInfo")
- public ModelAndView getIndex(){
- ModelAndView mav = new ModelAndView("index");
- User user = userService.selectUserById(1);
- mav.addObject("user", user);
- return mav;
- }
- }
11 User2Mapper.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.zheng.dao.UserDao">
- <resultMap id="BaseResultMap" type="com.zheng.model.User">
- <id column="USER_ID" property="userId" jdbcType="INTEGER" />
- <result column="USER_NAME" property="userName" jdbcType="CHAR" />
- <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />
- </resultMap>
-
- <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">
- SELECT * FROM t_user WHERE USER_ID = #{userId}
- </select>
- </mapper>
12 mybatis-config.xml的完整代码为
- <?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>
- </configuration>
13 jdbc.properties的完整代码为
- jdbc_driverClassName=com.mysql.jdbc.Driver
- jdbc_url=jdbc:mysql://localhost:3306/mytest
- jdbc_username=root
- jdbc_password=root
14 spring-mvc.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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-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">
-
- <mvc:annotation-driven />
-
- <context:component-scan base-package="com.zheng.controller"/>
-
-
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>
- </beans>
15 application.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:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
-
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:properties/*.properties</value>
-
- </list>
- </property>
- </bean>
-
-
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-
- <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/learning" />
- <property name="username" value="root" />
- <property name="password" value="christmas258@" /> -->
-
- <property name="driverClassName">
- <value>${jdbc_driverClassName}</value>
- </property>
- <property name="url">
- <value>${jdbc_url}</value>
- </property>
- <property name="username">
- <value>${jdbc_username}</value>
- </property>
- <property name="password">
- <value>${jdbc_password}</value>
- </property>
- </bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage"
- value="com.zheng.dao" />
- </bean>
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath:mapper/*.xml"/>
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
- <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
- /> -->
- </bean>
-
-
- <context:component-scan base-package="com.zheng.service" />
-
- </beans>
16 index.jsp中的完整代码为:
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <html>
- <body>
- <h2>User Info</h2>
- 用户名: ${user.userName}<br>
- 密码: ${user.userPassword}<br>
- </body>
- </html>
17 将项目添加进Tomcat 7中并运行
在浏览器中输入
http://localhost:8080/maven_springmvc_mybatis_demo/showUserInfo
显示结果为
18 源码下载地址
CSDN:http://download.csdn.net/detail/haishu_zheng/9534859
Github:https://github.com/zhenghaishu/maven_springmvc_mybatis_demo
Maven+SpringMVC+Mybatis整合入门Demo
原文:http://www.cnblogs.com/grimm/p/6732746.html