Springmvc+spring+mybatis+mysql configuration Process

Source: Internet
Author: User
Tags generator log4j

Environment: Eclipse

Project directory:

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 Package:

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 "/>


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><!--configuration files for  spring and MyBatis  -->< Context-param><param-name>contextconfiglocation</param-name><param-value>classpath: hanxuanyuan/config/spring-mybatis.xml</param-value></context-param><!--  Coding Filters  --> <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><listener-class> org.springframework.web.context.contextloaderlistener</listener-class></listener><!--  Prevent spring memory Overflow listener  --><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><!--  Here can be configured as *.do, corresponding to struts ' suffix habits  -->< 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 "><!--  Auto Scan  --><context: Component-scan base-package= "Hanxuanyuan"  /><!--  Introducing Profile  --><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}"  /><!--  Initialize connection size  --><property  name= "InitialSize"  value= "${initialsize}" ></property><!--  Maximum number of connection pools  -->< Property name= "maxactive"  value= "${maxactive}" ></property><!--  Connection pool Max idle  --> <property name= "Maxidle"  value= "${maxidle}" ></property><!--  Connection pool min idle  --> <property name= "Minidle"  value= "${minidle}" ></property><!--  get connection max wait time  -- ><property name= "MaxWait " value=" ${maxwait} "></property></bean><!-- spring and MyBatis Perfect integration, Configuration map files that do not require MyBatis  --><bean id= "sqlsessionfactory"  class= " Org.mybatis.spring.SqlSessionFactoryBean "><property name=" DataSource " ref=" DataSource " / ><!--  Auto-scan mapping.xml file  --><property name= "mapperlocations"  value= "classpath: Hanxuanyuan/mapping/*.xml "></property></bean><!--the package name of the  dao interface, Spring automatically finds the class under it  --> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" ><property name= "basepackage " value=" Hanxuanyuan.dao " /><property name=" Sqlsessionfactorybeanname " value=" Sqlsessionfactory "></property></bean><!--  (transaction management) 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> &nbSp;    <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 these three files are mybatis-generator automatically generated, their own Baidu is good

How to use: (1) Open the DOS command window, go to the directory, configure the Generatorconfig.xml, run the command:

Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generatorconfig.xml-overwrite

(2) Copy the file to the corresponding location of the project


Jar Package Link: http://down.51cto.com/data/2302826


Project Source Link: Stay put


Do not recommend that you directly see the source code, their own reference to the above codes, to see if you can build out

This article is from "Linux rookie" blog, please be sure to keep this source http://asura1992.blog.51cto.com/8159058/1919362

Springmvc+spring+mybatis+mysql configuration Process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.