Maven dependencies:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifacti d>spring-context</artifactid> <version>4.1.6.RELEASE</version> </dependency> <depen
Dency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactId>spring-jdbc</artifactId> <version>4.1.6.
release</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> < ;d ependency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</ Artifactid> <VERSION>4.1.6.RELEASE</VERSION> </dependency> <!--http://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl--<dependen cy> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> < ;version>1.2</version> </dependency> <dependency> <groupid>org.springframework</gr oupid> <artifactId>spring-test</artifactId> <version>4.1.6.RELEASE</version> </depe Ndency> <!--mybatis-<dependency> <groupid>org.mybatis</gro
Upid> <artifactId>mybatis</artifactId> <version>3.2.1</version>
</dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </d
Ependency> <dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-ja va</artifactid> <version>5.1.26</version> </dependency> </depen Dencies>
MAVEN will then download the corresponding package.
Configuration one: Use Mybytis-config: This file can configure the alias of the class under the model package, or you can configure the mapper file for the mapped SQL statement:
Great wisdom as shown in the figure
Mybatis-config.xml file
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public
"-//mybatis.org//dtd Config 3.0//en"
"http://mybatis.org/dtd/ Mybatis-3-config.dtd ">
<configuration>
<typeAliases>
<package name=" Com.wenyan.model "/> <!--directly aliases the class name under model--
<!-- <typealias type=" Com.wenyan.model.User "alias=" _user "/>- <!--aliases-
<!-- <typealias type=" Com.wenyan.model.News "alias=" _news "/>-->
</typeAliases>
<mappers>
<mapper Resource= "Com/wenyan/mapper/userdao.xml"/>
<mapper resource= "Com/wenyan/mapper/newsdao.xml"/>
</mappers>
</configuration>
The file is then introduced into the spring configuration file: Where Jdbcdatasource is the data source, the data source can be in a variety of ways, either directly with JDBC, or with Ali's Druid
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "DataSource "ref=" Jdbcdatasource "/> <!--<property name=" mapperlocations "value=" Classpath:com/wenyan/mapper/*.xml " />--> <!--not via Mybatis-config file--<property name= "configlocation" value= "Classpath:mybati S-config.xml "></property> </bean> <bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer "> <!--scan Me.gacl.dao This package and all mapped interface classes under its sub-package--< Property Name= "Basepackage" value= "Com.wenyan.dao"/> <property name= "sqlsessionfactorybeanname" value= "SqlS Essionfactory "/> </bean> <!--Configure Spring transaction Manager--<bean id=" TransactionManager "class=" Org.spri Ngframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource "ref=" Jdbcdatasource " /> </bean> <tx:annotation-driven transaction-manager= "traNsactionmanager "/> <tx:advice id=" Transactionadvice "transaction-manager=" TransactionManager "> < ;!
--interceptors: assigning different kinds of transactions--<tx:attributes> <tx:method name= "add*" propagation= "REQUIRED"/> <tx:method name= "append*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "RE quired "/> <tx:method name=" save* "propagation=" REQUIRED "/> <tx:method name=" update* " propagation= "REQUIRED"/> <tx:method name= "modify*" propagation= "REQUIRED"/> <tx:me
Thod name= "get*" propagation= "SUPPORTS"/> <tx:method name= "find*" propagation= "SUPPORTS"/> <tx:method name= "load*" propagation= "SUPPORTS"/> <tx:method name= "search*" propagation= "SUPPORTS "/> <tx:method name=" datagrid* "propagation=" SUPPORTS "/> <tx:method name=" * "Propag ation= "SUPPORTS"/>
</tx:attributes> </tx:advice>
Then were the respective mapper files: Case Userdao.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= "Com.wenyan.dao.UserDao" >
<select id= "GetUser" parametertype= "User" Resulttype = "User" >
select * from employee WHERE name
</select>
<select id= "GetUserName" parametertype= " User "resulttype=" user ">
select name from Employee WHERE id=#{id}
</select>
<insert id=" AddUser "parametertype=" User "flushcache=" true ">
INSERT into Employee (id,name,salary) VALUES (#{id},#{name}, #{salary})
</insert>
<update id= "UpdateUser" parametertype= "User" >
update employee SET Name=#{name} WHERE Id=#{id}
</update>
<delete id= "deleteuser" parametertype= "int" >
DELETE from employee WHERE id=#{id}
</delete>
</mapper>
The configuration is roughly the case.
MyBatis several ways to implement batch operations and their comparison:
Manual mode:
@Test public
void Testinsertshoudong ()
{long Time=system.currenttimemillis ();
Sqlsession sqlsession =this.ss.opensession (executortype.batch); Be sure to use Executortype.batch if you use the default submission
// sqlsession sqlsession =this.ss.opensession () without parameters;
Userdao Udao =sqlsession.getmapper (userdao.class);
try{for
(int i=0;i<10000;i++)
{
user user=new User ();
User.setname ("SJDHFJK" +i);
User.setsalary (+);
Udao.adduser (user);
}
Sqlsession.commit ();
System.out.println (System.currenttimemillis ()-time);
}
catch (Exception e) {
sqlsession.rollback ();
}
finally {
sqlsession.close ();
}
}
The declarative transaction management of spring management is implemented by:
A transaction is declared in the server layer:
public void Inserall ()
{for
(int i=0;i<10000;i++)
{
user user=new User ();
User.setname ("SJDHFJK" +i);
User.setsalary (+);
Udo.adduser (user);
}
}
Test Type tests:
public void Inserall ()
{for
(int i=0;i<10000;i++)
{
user user=new User ();
User.setname ("SJDHFJK" +i);
User.setsalary (+);
Udo.adduser (user);
}
}
The third kind, splicing to insert:
To create a stitched statement:
Mapper.xml file:
<insert id= "Batchinsert" parametertype= "List" >
INSERT INTO Employee (name,salary) Values
<foreach collection= "list" item= "user" index= "index" separator= "," >
(#{user.name}, #{ User.salary})
</foreach>
</insert>
after declaring a transaction in the server layer: public
void Inserbatch ()
{
list<user> users=new arraylist<user> ();
for (int i=0;i<10000;i++)
{
user user=new User ();
User.setname ("SJDHFJK" +i);
User.setsalary (+);
Users.add (user);
}
Udo.batchinsert (users);
}
Test: @Test public
void Testinsertbatch ()
{
long time=system.currenttimemillis ();
Nsv.inserbatch ();
System.out.println (System.currenttimemillis ()-time);
}
Two data: One copy 50, one copy 20000:
Final result test:
Declaring a transactional link string manual
50 article 20,000 article 50 article 20,000 article 50 article 20,000 article
124 4787 145 1645 3671 1995 485 2057 9 294 1993 2202 625 1888 1961 326 2052 2045 9 293 2313 1976 9 338 1930 2081 8 269 1725 1965 15 1719 2429 9 343 1717 1970 8 295 2512 2055 7 1742 2043 6 732 1764 2351 7 284 2651 2521 8 277 2358 2741 7 298 1736 2002 6 311 1880 1994 7 945 1733 2069 7 267 12 1717 2037 7 286 1973
Test code:
Similar to this:
@Test public
void Testinsertall_10 ()
{
list<long> times=new arraylist<long> ();
for (int i=0;i<20;i++)
{
long time=system.currenttimemillis ();
Nsv.inserall ();
Long Time2=system.currenttimemillis ()-time;
Times.add (time2);
}
for (int i=0;i<times.size (); i++)
{
System.out.println (Times.get (i));
}
}
Summary: The method of connecting strings is very fast, and is similar to both manual and declarative.