標籤:springmvc spring mybatis mysql 搭建過程
環境:eclipse
項目目錄:
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/92/6A/wKiom1j_LX6BegKJAAC6NLMPkqY267.jpg-wh_500x0-wm_3-wmp_4-s_814015310.jpg" title="33.JPG" alt="wKiom1j_LX6BegKJAAC6NLMPkqY267.jpg-wh_50" />
jar包:
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/92/69/wKioL1j_LOqBaZPFAAEPHoazQk0615.jpg-wh_500x0-wm_3-wmp_4-s_98899579.jpg" style="float:none;" title="11.JPG" alt="wKioL1j_LOqBaZPFAAEPHoazQk0615.jpg-wh_50" />
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/92/6A/wKiom1j_LOvjf2EBAABM6ni8aug770.jpg-wh_500x0-wm_3-wmp_4-s_207711246.jpg" style="float:none;" title="22.JPG" alt="wKiom1j_LOvjf2EBAABM6ni8aug770.jpg-wh_50" />
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>Archetype Created Web Application</display-name><!-- Spring和mybatis的設定檔 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:hanxuanyuan/config/spring-mybatis.xml</param-value></context-param><!-- 編碼過濾器 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Spring監聽器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 防止Spring記憶體溢出監聽器 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- Spring MVC servlet --><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:hanxuanyuan/config/spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><!-- 此處可以可以配置成*.do,對應struts的尾碼習慣 --><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>/index.jsp</welcome-file></welcome-file-list></web-app>
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:p="http://www.springframework.org/schema/p"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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 自動掃描 --><context:component-scan base-package="hanxuanyuan" /><!-- 引入設定檔 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:hanxuanyuan/config/jdbc.properties" /></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><!-- 初始化串連大小 --><property name="initialSize" value="${initialSize}"></property><!-- 串連池最大數量 --><property name="maxActive" value="${maxActive}"></property><!-- 串連池最大空閑 --><property name="maxIdle" value="${maxIdle}"></property><!-- 串連池最小空閑 --><property name="minIdle" value="${minIdle}"></property><!-- 擷取串連最大等待時間 --><property name="maxWait" value="${maxWait}"></property></bean><!-- spring和MyBatis完美整合,不需要mybatis的配置對應檔 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自動掃描mapping.xml檔案 --><property name="mapperLocations" value="classpath:hanxuanyuan/mapping/*.xml"></property></bean><!-- DAO介面所在包名,Spring會自動尋找其下的類 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="hanxuanyuan.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- (交易管理)transaction manager, use JtaTransactionManager for global tx --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean></beans>
jdbc.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost/cecusername=rootpassword=root#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570initialSize=0#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570maxActive=20#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2maxIdle=20#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2minIdle=1#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4maxWait=60000
log4j.properties
#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522Blog4j.rootLogger=INFO,Console,File#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0log4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.Target=System.out#\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\u5F0Flog4j.appender.Console.layout = org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=[%c] - %m%n#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6log4j.appender.File = org.apache.log4j.RollingFileAppender#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55log4j.appender.File.File = logs/ssm.log#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0Flog4j.appender.File.MaxFileSize = 10MB# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7log4j.appender.File.Threshold = ALLlog4j.appender.File.layout = org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
UserController.java
package hanxuanyuan.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import hanxuanyuan.domain.User;import hanxuanyuan.service.UserService;@Controller@RequestMapping("/user")public class UserController {@Resourceprivate UserService userService;@RequestMapping("/showUser")public String showUser(Model model){User user = userService.selectByPrimaryKey("1") ;model.addAttribute("user",user) ;return "welcome" ;}}
UserService.java
package hanxuanyuan.service;import hanxuanyuan.domain.User;public interface UserService {void insert(User record); User selectByPrimaryKey(String id);}
UserServiceImpl.java
package hanxuanyuan.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import hanxuanyuan.dao.UserMapper;import hanxuanyuan.domain.User;@Service("userService")public class UserServiceImpl implements UserService{@Resourceprivate UserMapper userMapper ;@Overridepublic void insert(User record) {userMapper.insert(record) ;}@Overridepublic User selectByPrimaryKey(String id) {return userMapper.selectByPrimaryKey(id);}}
UserMapper.java
package hanxuanyuan.dao;import hanxuanyuan.domain.User;public interface UserMapper { int deleteByPrimaryKey(String id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(String id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);}
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="hanxuanyuan.dao.UserMapper" > <resultMap id="BaseResultMap" type="hanxuanyuan.domain.User" > <id column="id" property="id" jdbcType="VARCHAR" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="VARCHAR" /> <result column="role" property="role" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, username, password, age, role </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=VARCHAR} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.String" > delete from user where id = #{id,jdbcType=VARCHAR} </delete> <insert id="insert" parameterType="hanxuanyuan.domain.User" > insert into user (id, username, password, age, role) values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=VARCHAR}, #{role,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="hanxuanyuan.domain.User" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="username != null" > username, </if> <if test="password != null" > password, </if> <if test="age != null" > age, </if> <if test="role != null" > role, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=VARCHAR}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="age != null" > #{age,jdbcType=VARCHAR}, </if> <if test="role != null" > #{role,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="hanxuanyuan.domain.User" > update user <set > <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="age != null" > age = #{age,jdbcType=VARCHAR}, </if> <if test="role != null" > role = #{role,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=VARCHAR} </update> <update id="updateByPrimaryKey" parameterType="hanxuanyuan.domain.User" > update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, age = #{age,jdbcType=VARCHAR}, role = #{role,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR} </update></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配置過程