Jee, Spring, Spring MVC, MyBatis Learning (eight)

Source: Internet
Author: User
Tags aop bind generator json xmlns

MyBatis Configuration

Reprint please declare reprint Source: http://blog.csdn.net/qq5132834/article/details/52227666

In the previous section, referring to the use of an important tool "Mybatis-generator" in MyBatis, this section will cover the configuration of "MyBatis". The content Code of this section continues to be written on the basis of section fifth, "Separating spring, Spring MVC configuration Files", download address: http://download.csdn.net/detail/qq5132834/9602553


1, the introduction of "MyBatis" must be the jar package, respectively:

Mybatis-3.1.1.jar

Mybatis-spring-1.1.1.jar

Druid-0.2.10.jar

Aspectjweaver-1.7.0.jar

Aopalliance-1.0.jar

Mysql-connector-java-5.1.21.jar.

The above jar packages are added to "web-inf/lib".

2, according to the previous lecture mentioned in the "SRC" in the new three packages:"Com.soft.model", "com.soft.mapping", "Sqlmap"

3, in the "Src/sources" folder to add "spring-mybatis.xml", the contents are as follows:

<?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:tx= "Http://www.springframework.org/schema/tx" 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/TX/http Www.springframework.org/schema/tx/spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!--jndi configuration Data Source--<!--<bean id=" DataSource "class=" Org.springframework.jndi.JndiObjectFactoryBean "> <property name=" jndiname "value=" ${ Jndiname} ></property> </bean>-<!--configuration data source-<bean name= "DataSource" class= "Com.alibab A.druid.pool.druiddatasource "init-method=" Init "destroy-method=" close "> <property name=" url "value="Jdbc:mysql://localhost:3306/test?characterencoding=utf8 "/> <property name=" username "value=" root "/> < Property name= "Password" value= "root"/> </bean> <!--mybatis file--<bean id= "Sqlsessionfactory" CLA ss= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "DataSource"/> <!-- Automatically scan the entity directory, omit configuration.xml manually configured--<property name= "mapperlocations" value= "Classpath:sqlmap/*.xml"/ > </bean> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= " Basepackage "value=" com.soft.mapping "/> <property name=" sqlsessionfactorybeanname "value=" Sqlsessionfactory " /> </bean> <!--configuration transaction Manager-<bean name= "TransactionManager" class= "Org.springframework.jdbc.dataso Urce. Datasourcetransactionmanager "> <property name=" dataSource "ref=" DataSource "/> </bean> <!--annotation configuration things Things--<!--<tx:annotation-driven Transaction-manager= "TransactionManager"/>-<!--Interceptor configuration Things--<tx:advice id= "Transactionadvice" Transa
			Ction-manager= "TransactionManager" > <tx:attributes> <tx:method name= "add*" propagation= "REQUIRED"/>
			<tx:method name= "append*" propagation= "REQUIRED"/> <tx:method name= "save*" propagation= "REQUIRED"/>
			<tx:method name= "update*" propagation= "REQUIRED"/> <tx:method name= "modify*" propagation= "REQUIRED"/> <tx:method name= "edit*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "REQUIRED"/> & Lt;tx:method name= "delete*" propagation= "REQUIRED"/> <tx:method name= "remove*" propagation= "REQUIRED"/> & Lt;tx:method name= "Repair" propagation= "REQUIRED"/> <tx:method name= "get*" propagation= "REQUIRED" read-only= "t Rue "/> <tx:method name=" find* "propagation=" REQUIRED "read-only=" true "/> <tx:method name=" load* "Propa gation= "REQUIRED" read-only= "true"/> <tx:method name= "search*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "Datagri 
		d* "propagation=" REQUIRED "read-only=" true "/> <tx:method name=" * "propagation=" REQUIRED "read-only=" true "/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id= "Transactionpointcut" Expre ssion= "Execution (* com.zuk.services). *impl.* (..)) " 
	 

/> <aop:advisor pointcut-ref= "transactionpointcut" advice-ref= "Transactionadvice"/> </aop:config> </beans>

Note the contents of the "Spring-mubatis.xml" file:

3.1. Data source

	<!--configuration Data source-
	<bean name= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" init-method= " Init "destroy-method=" close ">
		<property name=" url "value=" Jdbc:mysql://localhost:3306/test? Characterencoding=utf8 "/>
		<property name=" username "value=" root "/>
		<property name=" Password " value= "root"/>
	</bean>

If you do not want to add a "Druid-0.2.10.jar" package, you can use Springframework database connection instead.

        <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/test?characterencoding=utf8 "/>
		<property name=" username "value=" root "/>
		< Property name= "Password" value= "root"/> 
	</bean>


3.2. mybatis file configuration, "xml" file under "Sqlmap" folder:

	<!--mybatis file--
	<bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
		<property name= "DataSource" ref= "DataSource"/>
		<!--automatically scan the entity directory, omit configuration.xml manual configuration--
		<property name= "mapperlocations" value= "Classpath:sqlmap/*.xml"/>
	</bean>

<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
		<property name= "Basepackage" value= "Com.soft.mapping"/>
		<property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory"/>
	</bean>

3.3, this configuration does not know whether to configure, but to configure, will not error.

Note: "Expression =" Execution (* com.zuk.services). *impl.* (..)) " The contents of the "<context:component-scan base-package="com.zuk.services"/>" configuration to be configured with the file in "Spring.xml" are consistent:

	<aop:config>
		<aop:pointcut id= "transactionpointcut" expression= "Execution (* com.zuk.services). *impl.* (..)) " />
		<aop:advisor pointcut-ref= "transactionpointcut" advice-ref= "Transactionadvice"/>
	


4, add "spring-mybiatis.xml" file to "web. xml" file, the file path is: "classpath:sources/spring-mybatis.xml"

  <!--load Spring configuration Files--
  <context-param>
		<param-name>contextconfiglocation</param-name >
		<param-value>classpath:sources/spring.xml,classpath:sources/spring-mybatis.xml</param-value >
  </context-param>

5, the previous section through the "Mybatis-generator" tool to generate Java, XML files, copied to the corresponding package.

6. Create a new "Loginservice.java" class in "Com.zuk.services". The code is as follows:

Package com.zuk.services;

Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;

Import Com.soft.mapping.LoginMapper;
Import Com.soft.model.Login;

@Service ("Loginservice") public
class Loginservice {

	@Autowired
	private loginmapper loginmapper;
	
	Public login GetLogin (String userid) {
		Login login = This.loginMapper.selectByPrimaryKey (userid);
		return login;
	}
	
}

7, modify the "Logincontroller.java" class, in which the data is introduced as follows:

@Autowired public
Loginservice Loginservice;
The complete code is as follows:

Package com.zuk.controllers;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import javax.servlet.http.HttpSession;
Import Org.nutz.json.Json;
Import Org.nutz.lang.util.NutMap;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import Org.springframework.web.bind.annotation.ModelAttribute;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;

Import Org.springframework.web.bind.annotation.ResponseBody;
Import Com.soft.model.Login;
Import Com.zuk.model.User;
Import Com.zuk.services.LoginService;

Import Com.zuk.services.UserService;
	 
	/** * @author 513283439@qq.com * * */@Controller @RequestMapping (value= "Logincontroller") public class Logincontroller {
	
	@Autowired public UserService UserService;

	@Autowired public Loginservice Loginservice; @RequestMapping (value ="/login.xhtml") public String Login (HttpSession ession, httpservletrequest request, HttpServletResponse Respo  
		NSE, model model) {return "/jsp/login";
		 /** * The actual path of the jump is calculated on the mvc-servlet.xml configured in the * prefix:/web-inf; * suffix:. jsp * Actual path:/web-inf/jsp/login.jsp * */}/** * <br> mainly solve two problems: * <br>1, Post methods Chinese garbled problem; * &LT
	 Br>2, the value of the Name property inside the front-end form is mapped directly to the Java object. * */@RequestMapping (value = "/actionform.xhtml") public String actionform (HttpSession ession, HttpServletRequest req Uest, httpservletresponse response, model model, @ModelAttribute user user) {//system.out.println (person
		. Getusid ());
		System.out.println (Person.getpawd ());
		
		System.out.println (Person.getcomp ());
		Model.addattribute ("Usid", User.getusid ());
		Model.addattribute ("Pawd", User.getpawd ());
		
		Model.addattribute ("comp", User.getcomp ());
		if (This.userService.isExist (User.getusid (), User.getpawd ())) {return '/jsp/main '; }else{return "/jsp/Error "; }}/** *<br> main solution Ajax request return JSON data hard * */@ResponseBody @RequestMapping (value = "/ajaxform.json", produce s = "Text/html;charset=utf-8") public String ajaxform (HttpSession ession, httpservletrequest request, Httpservletre
		Sponse response, model model, @ModelAttribute user user) {//system.out.println (Person.getusid ());
		System.out.println (Person.getpawd ());
		
		System.out.println (Person.getcomp ());
		Model.addattribute ("Usid", User.getusid ());
		Model.addattribute ("Pawd", User.getpawd ());
		
		
		Model.addattribute ("comp", User.getcomp ());
		
		Nutmap result = Nutmap.new ();
		
		
		Result.put ("Data", user);
		if (This.userService.isExist (User.getusid (), User.getpawd ())) {Result.put ("success", true);
		}else{result.put ("Success", false);
		Login login = This.loginService.getLogin ("b180121");
		
		Result.put ("Login", login);
		
		String JSON = Json.tojson (result);
		
		SYSTEM.OUT.PRINTLN (JSON);
	return JSON;

 }
	
	
}

8, publish the project, enter the link: http://localhost:8080/ZZZ/LoginController/login.xhtml click the "Ajax" button.

9. Source code: http://download.csdn.net/detail/qq5132834/9607606

10, finally, for the database configuration, you can refer to the "fifth section".


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.