当前位置:Gxlcms > 数据库问题 > springmvc+spring+mybatis+mysql配置过程

springmvc+spring+mybatis+mysql配置过程

时间:2021-07-01 10:21:17 帮助过:23人阅读

环境:eclipse

项目目录:

技术分享

jar包:

技术分享

技术分享


web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <display-name>Archetype Created Web Application</display-name>
  7. <!-- Spring和mybatis的配置文件 -->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:hanxuanyuan/config/spring-mybatis.xml</param-value>
  11. </context-param>
  12. <!-- 编码过滤器 -->
  13. <filter>
  14. <filter-name>encodingFilter</filter-name>
  15. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  16. <async-supported>true</async-supported>
  17. <init-param>
  18. <param-name>encoding</param-name>
  19. <param-value>UTF-8</param-value>
  20. </init-param>
  21. </filter>
  22. <filter-mapping>
  23. <filter-name>encodingFilter</filter-name>
  24. <url-pattern>/*</url-pattern>
  25. </filter-mapping>
  26. <!-- Spring监听器 -->
  27. <listener>
  28. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  29. </listener>
  30. <!-- 防止Spring内存溢出监听器 -->
  31. <listener>
  32. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  33. </listener>
  34. <!-- Spring MVC servlet -->
  35. <servlet>
  36. <servlet-name>SpringMVC</servlet-name>
  37. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  38. <init-param>
  39. <param-name>contextConfigLocation</param-name>
  40. <param-value>classpath:hanxuanyuan/config/spring-mvc.xml</param-value>
  41. </init-param>
  42. <load-on-startup>1</load-on-startup>
  43. <async-supported>true</async-supported>
  44. </servlet>
  45. <servlet-mapping>
  46. <servlet-name>SpringMVC</servlet-name>
  47. <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
  48. <url-pattern>/</url-pattern>
  49. </servlet-mapping>
  50. <welcome-file-list>
  51. <welcome-file>/index.jsp</welcome-file>
  52. </welcome-file-list>
  53. </web-app>


spring-mybatis.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"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8. http://www.springframework.org/schema/context  
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  10. http://www.springframework.org/schema/mvc  
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  12. <!-- 自动扫描 -->
  13. <context:component-scan base-package="hanxuanyuan" />
  14. <!-- 引入配置文件 -->
  15. <bean id="propertyConfigurer"
  16. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  17. <property name="location" value="classpath:hanxuanyuan/config/jdbc.properties" />
  18. </bean>
  19. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  20. destroy-method="close">
  21. <property name="driverClassName" value="${driver}" />
  22. <property name="url" value="${url}" />
  23. <property name="username" value="${username}" />
  24. <property name="password" value="${password}" />
  25. <!-- 初始化连接大小 -->
  26. <property name="initialSize" value="${initialSize}"></property>
  27. <!-- 连接池最大数量 -->
  28. <property name="maxActive" value="${maxActive}"></property>
  29. <!-- 连接池最大空闲 -->
  30. <property name="maxIdle" value="${maxIdle}"></property>
  31. <!-- 连接池最小空闲 -->
  32. <property name="minIdle" value="${minIdle}"></property>
  33. <!-- 获取连接最大等待时间 -->
  34. <property name="maxWait" value="${maxWait}"></property>
  35. </bean>
  36. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  37. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  38. <property name="dataSource" ref="dataSource" />
  39. <!-- 自动扫描mapping.xml文件 -->
  40. <property name="mapperLocations" value="classpath:hanxuanyuan/mapping/*.xml"></property>
  41. </bean>
  42. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  43. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  44. <property name="basePackage" value="hanxuanyuan.dao" />
  45. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  46. </bean>
  47. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  48. <bean id="transactionManager"
  49. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  50. <property name="dataSource" ref="dataSource" />
  51. </bean>
  52. </beans>

jdbc.properties

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost/cec
  3. username=root
  4. password=root
  5. #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
  6. initialSize=0
  7. #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
  8. maxActive=20
  9. #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
  10. maxIdle=20
  11. #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
  12. minIdle=1
  13. #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
  14. maxWait=60000


log4j.properties

  1. #\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B
  2. log4j.rootLogger=INFO,Console,File
  3. #\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Console.Target=System.out
  6. #\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
  7. log4j.appender.Console.layout = org.apache.log4j.PatternLayout
  8. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
  9. #\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
  10. log4j.appender.File = org.apache.log4j.RollingFileAppender
  11. #\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
  12. log4j.appender.File.File = logs/ssm.log
  13. #\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
  14. log4j.appender.File.MaxFileSize = 10MB
  15. # \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
  16. log4j.appender.File.Threshold = ALL
  17. log4j.appender.File.layout = org.apache.log4j.PatternLayout
  18. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

UserController.java

  1. package hanxuanyuan.controller;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import hanxuanyuan.domain.User;
  7. import hanxuanyuan.service.UserService;
  8. @Controller
  9. @RequestMapping("/user")
  10. public class UserController {
  11. @Resource
  12. private UserService userService;
  13. @RequestMapping("/showUser")
  14. public String showUser(Model model){
  15. User user = userService.selectByPrimaryKey("1") ;
  16. model.addAttribute("user",user) ;
  17. return "welcome" ;
  18. }
  19. }

UserService.java

  1. package hanxuanyuan.service;
  2. import hanxuanyuan.domain.User;
  3. public interface UserService {
  4. void insert(User record);
  5.  User selectByPrimaryKey(String id);
  6. }


UserServiceImpl.java


  1. package hanxuanyuan.service;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Service;
  4. import hanxuanyuan.dao.UserMapper;
  5. import hanxuanyuan.domain.User;
  6. @Service("userService")
  7. public class UserServiceImpl implements UserService{
  8. @Resource
  9. private UserMapper userMapper ;
  10. @Override
  11. public void insert(User record) {
  12. userMapper.insert(record) ;
  13. }
  14. @Override
  15. public User selectByPrimaryKey(String id) {
  16. return userMapper.selectByPrimaryKey(id);
  17. }
  18. }


UserMapper.java

  1. package hanxuanyuan.dao;
  2. import hanxuanyuan.domain.User;
  3. public interface UserMapper {
  4.     int deleteByPrimaryKey(String id);
  5.     int insert(User record);
  6.     int insertSelective(User record);
  7.     User selectByPrimaryKey(String id);
  8.     int updateByPrimaryKeySelective(User record);
  9.     int updateByPrimaryKey(User record);
  10. }


UserMapper.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="hanxuanyuan.dao.UserMapper" >
  4.   <resultMap id="BaseResultMap" type="hanxuanyuan.domain.User" >
  5.     <id column="id" property="id" jdbcType="VARCHAR" />
  6.     <result column="username" property="username" jdbcType="VARCHAR" />
  7.     <result column="password" property="password" jdbcType="VARCHAR" />
  8.     <result column="age" property="age" jdbcType="VARCHAR" />
  9.     <result column="role" property="role" jdbcType="VARCHAR" />
  10.   </resultMap>
  11.   <sql id="Base_Column_List" >
  12.     id, username, password, age, role
  13.   </sql>
  14.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
  15.     select 
  16.     <include refid="Base_Column_List" />
  17.     from user
  18.     where id = #{id,jdbcType=VARCHAR}
  19.   </select>
  20.   <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
  21.     delete from user
  22.     where id = #{id,jdbcType=VARCHAR}
  23.   </delete>
  24.   <insert id="insert" parameterType="hanxuanyuan.domain.User" >
  25.     insert into user (id, username, password, 
  26.       age, role)
  27.     values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
  28.       #{age,jdbcType=VARCHAR}, #{role,jdbcType=VARCHAR})
  29.   </insert>
  30.   <insert id="insertSelective" parameterType="hanxuanyuan.domain.User" >
  31.     insert into user
  32.     <trim prefix="(" suffix=")" suffixOverrides="," >
  33.       <if test="id != null" >
  34.         id,
  35.       </if>
  36.       <if test="username != null" >
  37.         username,
  38.       </if>
  39.       <if test="password != null" >
  40.         password,
  41.       </if>
  42.       <if test="age != null" >
  43.         age,
  44.       </if>
  45.       <if test="role != null" >
  46.         role,
  47.       </if>
  48.     </trim>
  49.     <trim prefix="values (" suffix=")" suffixOverrides="," >
  50.       <if test="id != null" >
  51.         #{id,jdbcType=VARCHAR},
  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="age != null" >
  60.         #{age,jdbcType=VARCHAR},
  61.       </if>
  62.       <if test="role != null" >
  63.         #{role,jdbcType=VARCHAR},
  64.       </if>
  65.     </trim>
  66.   </insert>
  67.   <update id="updateByPrimaryKeySelective" parameterType="hanxuanyuan.domain.User" >
  68.     update user
  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="age != null" >
  77.         age = #{age,jdbcType=VARCHAR},
  78.       </if>
  79.       <if test="role != null" >
  80.         role = #{role,jdbcType=VARCHAR},
  81.       </if>
  82.     </set>
  83.     where id = #{id,jdbcType=VARCHAR}
  84.   </update>
  85.   <update id="updateByPrimaryKey" parameterType="hanxuanyuan.domain.User" >
  86.     update user
  87.     set username = #{username,jdbcType=VARCHAR},
  88.       password = #{password,jdbcType=VARCHAR},
  89.       age = #{age,jdbcType=VARCHAR},
  90.       role = #{role,jdbcType=VARCHAR}
  91.     where id = #{id,jdbcType=VARCHAR}
  92.   </update>
  93. </mapper>


userMapper,User,UserMapper.xml这三个文件是mybatis-generator自动生成的,自己百度就好

使用方法: (1)打开dos命令窗口,进入到该目录下,配置好generatorConfig.xml后,运行命令: 

 java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

      (2)拷贝文件到项目对应位置


jar包链接:http://down.51cto.com/data/2302826


项目源码链接: 待放


不推荐大家直接看源码,自己参考以上代码,看看能否自己搭出来

本文出自 “linux菜鸟” 博客,请务必保留此出处http://asura1992.blog.51cto.com/8159058/1919362

springmvc+spring+mybatis+mysql配置过程

标签:springmvc   spring   mybatis   mysql   搭建过程   

人气教程排行