一、新建javaweb项目,并建好相应的包结构



二、添加项目jar到lib目录下



三、在config包中新建配置文件

sping-mvc.xml,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  11. <!-- 注解扫描包 -->
  12. <context:component-scan base-package="com.hyg.im" />
  13. <!-- 开启注解 -->
  14. <mvc:annotation-driven />
  15. </beans>

spring-common.xml内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  12. <!-- 1. 数据源 : DriverManagerDataSource -->
  13. <bean id="dataSource"
  14. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  15. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  16. <property name="url" value="jdbc:mysql://localhost:3306/test" />
  17. <property name="username" value="root" />
  18. <property name="password" value="123456" />
  19. </bean>
  20. <!--
  21. 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
  22. MyBatis定义数据源,同意加载配置
  23. -->
  24. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  25. <property name="dataSource" ref="dataSource"></property>
  26. </bean>
  27. <!--
  28. 3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
  29. basePackage:指定sql映射文件/接口所在的包(自动扫描)
  30. -->
  31. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  32. <property name="basePackage" value="com.hyg.im.mapper"></property>
  33. <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  34. </bean>
  35. <!--
  36. 4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
  37. -->
  38. <bean id="txManager"
  39. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  40. <property name="dataSource" ref="dataSource"></property>
  41. </bean>
  42. <!-- 5. 使用声明式事务
  43. transaction-manager:引用上面定义的事务管理器
  44. -->
  45. <tx:annotation-driven transaction-manager="txManager" />
  46. </beans>

四、配置web.xml,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!-- 加载Spring容器配置 -->
  7. <listener>
  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  9. </listener>
  10. <!-- 设置Spring容器加载所有的配置文件的路径 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath*:config/spring-*.xml</param-value>
  14. </context-param>
  15. <!-- 配置SpringMVC核心控制器 -->
  16. <servlet>
  17. <servlet-name>springMVC</servlet-name>
  18. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  19. <!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 -->
  20. <init-param>
  21. <param-name>contextConfigLocation</param-name>
  22. <param-value>classpath*:config/spring-mvc.xml</param-value>
  23. </init-param>
  24. <!-- 启动加载一次 -->
  25. <load-on-startup>1</load-on-startup>
  26. </servlet>
  27. <!--为DispatcherServlet建立映射 -->
  28. <servlet-mapping>
  29. <servlet-name>springMVC</servlet-name>
  30. <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
  31. <url-pattern>/</url-pattern>
  32. </servlet-mapping>
  33. <servlet-mapping>
  34. <servlet-name>default</servlet-name>
  35. <url-pattern>*.html</url-pattern>
  36. </servlet-mapping>
  37. <servlet-mapping>
  38. <servlet-name>default</servlet-name>
  39. <url-pattern>*.gif</url-pattern>
  40. </servlet-mapping>
  41. <servlet-mapping>
  42. <servlet-name>default</servlet-name>
  43. <url-pattern>*.css</url-pattern>
  44. </servlet-mapping>
  45. <servlet-mapping>
  46. <servlet-name>default</servlet-name>
  47. <url-pattern>*.js</url-pattern>
  48. </servlet-mapping>
  49. <servlet-mapping>
  50. <servlet-name>default</servlet-name>
  51. <url-pattern>*.jpg</url-pattern>
  52. </servlet-mapping>
  53. <servlet-mapping>
  54. <servlet-name>default</servlet-name>
  55. <url-pattern>*.png</url-pattern>
  56. </servlet-mapping>
  57. <!-- 防止Spring内存溢出监听器 -->
  58. <listener>
  59. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  60. </listener>
  61. <!-- 解决工程编码过滤器 -->
  62. <filter>
  63. <filter-name>encodingFilter</filter-name>
  64. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  65. <init-param>
  66. <param-name>encoding</param-name>
  67. <param-value>UTF-8</param-value>
  68. </init-param>
  69. <init-param>
  70. <param-name>forceEncoding</param-name>
  71. <param-value>true</param-value>
  72. </init-param>
  73. </filter>
  74. <filter-mapping>
  75. <filter-name>encodingFilter</filter-name>
  76. <url-pattern>/*</url-pattern>
  77. </filter-mapping>
  78. <welcome-file-list>
  79. <welcome-file>login.jsp</welcome-file>
  80. </welcome-file-list>
  81. </web-app>

五、在mysql中创建数据库test,并新建表user_info,字段如下



向user_info中添加几条数据,如下



六、用mybatis生成工具,生成实体类、映射文件

实体类UserInfo.java代码如下:

  1. package com.hyg.im.model;
  2. import java.util.Date;
  3. public class UserInfo {
  4. private Integer userId;
  5. private String userName;
  6. private String password;
  7. private String trueName;
  8. private Date addedTime;
  9. public Integer getUserId() {
  10. return userId;
  11. }
  12. public void setUserId(Integer userId) {
  13. this.userId = userId;
  14. }
  15. public String getUserName() {
  16. return userName;
  17. }
  18. public void setUserName(String userName) {
  19. this.userName = userName == null ? null : userName.trim();
  20. }
  21. public String getPassword() {
  22. return password;
  23. }
  24. public void setPassword(String password) {
  25. this.password = password == null ? null : password.trim();
  26. }
  27. public String getTrueName() {
  28. return trueName;
  29. }
  30. public void setTrueName(String trueName) {
  31. this.trueName = trueName == null ? null : trueName.trim();
  32. }
  33. public Date getAddedTime() {
  34. return addedTime;
  35. }
  36. public void setAddedTime(Date addedTime) {
  37. this.addedTime = addedTime;
  38. }
  39. }

UserInfoMapper.java代码如下:

  1. package com.hyg.im.mapper;
  2. import com.hyg.im.model.UserInfo;
  3. public interface UserInfoMapper {
  4. int deleteByPrimaryKey(Integer userId);
  5. int insert(UserInfo record);
  6. int insertSelective(UserInfo record);
  7. UserInfo selectByPrimaryKey(Integer userId);
  8. int updateByPrimaryKeySelective(UserInfo record);
  9. int updateByPrimaryKey(UserInfo record);
  10. }

UserInfoMapper.xml代码如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.hyg.im.mapper.UserInfoMapper" >
  4. <resultMap id="BaseResultMap" type="com.hyg.im.model.UserInfo" >
  5. <id column="userId" property="userId" jdbcType="INTEGER" />
  6. <result column="userName" property="userName" jdbcType="VARCHAR" />
  7. <result column="password" property="password" jdbcType="VARCHAR" />
  8. <result column="trueName" property="trueName" jdbcType="VARCHAR" />
  9. <result column="addedTime" property="addedTime" jdbcType="DATE" />
  10. </resultMap>
  11. <sql id="Base_Column_List" >
  12. userId, userName, password, trueName, addedTime
  13. </sql>
  14. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  15. select
  16. <include refid="Base_Column_List" />
  17. from user_info
  18. where userId = #{userId,jdbcType=INTEGER}
  19. </select>
  20. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  21. delete from user_info
  22. where userId = #{userId,jdbcType=INTEGER}
  23. </delete>
  24. <insert id="insert" parameterType="com.hyg.im.model.UserInfo" >
  25. insert into user_info (userId, userName, password,
  26. trueName, addedTime)
  27. values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
  28. #{trueName,jdbcType=VARCHAR}, #{addedTime,jdbcType=DATE})
  29. </insert>
  30. <insert id="insertSelective" parameterType="com.hyg.im.model.UserInfo" >
  31. insert into user_info
  32. <trim prefix="(" suffix=")" suffixOverrides="," >
  33. <if test="userId != null" >
  34. userId,
  35. </if>
  36. <if test="userName != null" >
  37. userName,
  38. </if>
  39. <if test="password != null" >
  40. password,
  41. </if>
  42. <if test="trueName != null" >
  43. trueName,
  44. </if>
  45. <if test="addedTime != null" >
  46. addedTime,
  47. </if>
  48. </trim>
  49. <trim prefix="values (" suffix=")" suffixOverrides="," >
  50. <if test="userId != null" >
  51. #{userId,jdbcType=INTEGER},
  52. </if>
  53. <if test="userName != null" >
  54. #{userName,jdbcType=VARCHAR},
  55. </if>
  56. <if test="password != null" >
  57. #{password,jdbcType=VARCHAR},
  58. </if>
  59. <if test="trueName != null" >
  60. #{trueName,jdbcType=VARCHAR},
  61. </if>
  62. <if test="addedTime != null" >
  63. #{addedTime,jdbcType=DATE},
  64. </if>
  65. </trim>
  66. </insert>
  67. <update id="updateByPrimaryKeySelective" parameterType="com.hyg.im.model.UserInfo" >
  68. update user_info
  69. <set >
  70. <if test="userName != null" >
  71. userName = #{userName,jdbcType=VARCHAR},
  72. </if>
  73. <if test="password != null" >
  74. password = #{password,jdbcType=VARCHAR},
  75. </if>
  76. <if test="trueName != null" >
  77. trueName = #{trueName,jdbcType=VARCHAR},
  78. </if>
  79. <if test="addedTime != null" >
  80. addedTime = #{addedTime,jdbcType=DATE},
  81. </if>
  82. </set>
  83. where userId = #{userId,jdbcType=INTEGER}
  84. </update>
  85. <update id="updateByPrimaryKey" parameterType="com.hyg.im.model.UserInfo" >
  86. update user_info
  87. set userName = #{userName,jdbcType=VARCHAR},
  88. password = #{password,jdbcType=VARCHAR},
  89. trueName = #{trueName,jdbcType=VARCHAR},
  90. addedTime = #{addedTime,jdbcType=DATE}
  91. where userId = #{userId,jdbcType=INTEGER}
  92. </update>
  93. </mapper>

注意:UserInfoMapper.java和UserInfoMapper.xml的文件名需要一样,并放入包com.hyg.im.mapper中,UserInfo.java放入包com.hyg.im.model中

七、添加业务层方法,查询所有用户信息

接口UserInfoService.java代码如下:

  1. package com.hyg.im.service;
  2. import java.util.List;
  3. import com.hyg.im.model.UserInfo;
  4. public interface UserInfoService {
  5. /**
  6. * 查询所有用户信息
  7. */
  8. List<UserInfo> findUserInfoList();
  9. }

接口实现类UserInfoServiceImpl.java代码如下:

  1. package com.hyg.im.service.impl;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import com.hyg.im.mapper.UserInfoMapper;
  7. import com.hyg.im.model.UserInfo;
  8. import com.hyg.im.service.UserInfoService;
  9. @Service
  10. @Transactional //此处不再进行创建SqlSession和提交事务,都已交由spring去管理了。
  11. public class UserInfoServiceImpl implements UserInfoService {
  12. @Resource
  13. private UserInfoMapper mapper;
  14. @Override
  15. public List<UserInfo> findUserInfoList() {
  16. return mapper.findUserInfoList();
  17. }
  18. }

八、添加Dao层方法

UserInfoMapper.java中同样加入 findUserInfoList()接口

代码如下:

  1. List<UserInfo> findUserInfoList();

UserInfoMapper.xml中加入查询sql语句与之对应

  1. <select id="findUserInfoList" resultMap="BaseResultMap" parameterType="map" >
  2. select
  3. <include refid="Base_Column_List" />
  4. from user_info
  5. </select>

九、添加controller层代码

在包com.hyg.im.controller中新建UserInfoController.java,代码如下:

  1. package com.hyg.im.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import com.hyg.im.model.UserInfo;
  10. import com.hyg.im.service.UserInfoService;
  11. @Controller
  12. //@RequestMapping("/user")
  13. public class UserInfoController{
  14. @Autowired
  15. private UserInfoService userInfoService;
  16. /**
  17. * 查询用户列表
  18. */
  19. @RequestMapping("/findUserInfoList")
  20. public ModelAndView findUserInfoList(HttpServletRequest request,HttpServletResponse response){
  21. ModelAndView mav = new ModelAndView();
  22. mav.setViewName("/userInfoList.jsp"); //返回的文件路径
  23. List<UserInfo> userInfoList = userInfoService.findUserInfoList();
  24. mav.addObject("userInfoList", userInfoList);
  25. return mav;
  26. }
  27. }

十、添加view视图层代码

在WebContent根目录新建文件userInfoList.jsp,用于展示用户数据,代码如下:

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>用户列表</title>
  8. </head>
  9. <body>
  10. <table border="1">
  11. <tr>
  12. <td>用户ID</td><td>用户名</td><td>真实姓名</td>
  13. </tr>
  14. <c:forEach var="row" items="${userInfoList}">
  15. <tr>
  16. <td>${row.userId }</td><td>${row.userName }</td><td>${row.trueName}</td>
  17. </tr>
  18. </c:forEach>
  19. </table>
  20. </body>
  21. </html>

十一、发布项目到Tomcat

项目右键/Run As/Run On Server,将项目发布到Tomcat

十二、浏览器访问,查看效果

访问地址:http://localhost:8080/ssm/findUserInfoList



到此,springmvc+spring+mybatis框架搭建成功。

项目最终的目录结构如下图:

SpringMVC+Mybatis框架搭建的更多相关文章

  1. SSM(Spring +SpringMVC + Mybatis)框架搭建

    SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...

  2. spring+springmvc+mybatis框架搭建

    一.开发前准备 1)ecplise4.11.0 百度网盘:https://pan.baidu.com/s/1wO9_I52lp0mYNeNTdnj80w 提取码:booa 2)jdk1.6.0_45  ...

  3. 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤

    一.整体概览 首先看maven工程的创建 二.各层的文件配置 2.1,SSM父工程 <span style="font-family:KaiTi_GB2312;font-size:18 ...

  4. 基于Maven的ssm(spring+springMvc+Mybatis)框架搭建

    前言 本demo是在idea下搭建的maven项目,数据库使用Mysql,jdk版本是1.8.0_171,ideal:2017.3.5 一.新建项目 1.file->new->porjec ...

  5. SpringMvc+Mybatis 框架搭建

    本文承接上一篇[idea使用maven搭建springmvc] 开篇:在main/resources下新建dbconfig.properties.spring.xml.spring-mybatis.x ...

  6. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  7. 使用intellij idea搭建MAVEN+springmvc+mybatis框架

    原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...

  8. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc

    在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件

  9. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试

    这一部分的主要目的是 配置spring-service.xml  也就是配置spring  并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+M ...

随机推荐

  1. eclipse 误删文件的恢复,代码的恢复

    误删除文件的恢复 在用eclipse进行代码编写操作时,有时会误删除文件或者文件包.通过eclipse的恢复文件功能可以恢复误删除的文件. 具体步骤为: 1.选择误删除文件在eclipse所在包(文件 ...

  2. Excel VBA 复制

    将 Sheet1 复制到 Sheet3 后面时,实现方法如下: Worksheets("Sheet1").Copy After:=Worksheets("Sheet3&q ...

  3. SpringMVC方法接收参数可以为空、默认值设置

  4. 【量产工具修复】U盘插上没反应,格式化提示有写保护

    最近在实验室发现师兄留下的U盘,插上电脑后打不开,弹出格式化界面,格式化的时候又提示该u盘“被写保护无法格式化”,于是打算采用量产的方法. 第一步:使用chipgenius监测u盘的芯片制造商和型号 ...

  5. OC category(分类)

    // ()代表着是一个分类 // ()中的Test代表着分类的名称 @interface Student (Test) // 分类只能扩展方法,不能增加成员变量 - (void)test2; @end

  6. 设计模式——代理模式(Proxy Pattern)

    代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. UML图: 模型设计: Subject类: package com.cnblog.clarck; /** * Subject 类 ...

  7. 记忆化搜索,FatMouse and Cheese

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1107 http://acm.hdu.edu.cn/showpro ...

  8. UVA515 King

    嘟嘟嘟 题目翻译:有n个数,m个限制条件.每一个限制条件形如:1.x y gt c:表示ax + ax+1 + … +ay > c.2.x y It c:表示ax + ax+1 + …… +ay ...

  9. 【题解】洛谷P2426删数

    链接 https://www.luogu.org/problemnew/show/P2426 念念碎 第一次接触到区间DP(瑟瑟发抖) 所以象征性地看了一下题解 这好像是一道比较基础的区间DP吧 但是 ...

  10. opencv 数据类型转换:CvArr, Mat, CvMat, IplImage, BYTE 转

    留着以后查询: http://blog.csdn.net/augusdi/article/details/8863820 一.Mat类型:矩阵类型,Matrix. 在openCV中,Mat是一个多维的 ...