3. Optimize Mybatis

A disposed in a single database connection properties file

 1, a new file in the db.properties resources directory, as shown below:

Db.properties file written in connection to the database requires a database driver, connection URL address, user name, password, as follows:

1  # data source information
 2  jdbc.driver = com.mysql.jdbc.Driver
 . 3  jdbc.url = JDBC: MySQL: // localhost: 3306 / MyBatis
 . 4  jdbc.username = the root
 . 5 jdbc.password = the root

 

2, reference file db.properties the MyBatis conf.xml file as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <!-- 引入属性文件 -->
 6     <properties resource="db.properties"></properties>
 7     <!-- 为实体类起别名 -->
 8     <typeAliases>
 9         <!-- <typeAlias type="com.zhiyou100.wc.bean.Users" alias="u"/> --> <!--
          
     
     
         
              
             
             -->
17                 <property name="driver" value="${jdbc.driver}" />
18                 <property name="url"
19                     value="${jdbc.url}" />
20                 <property name="username" value="${jdbc.username}" />
21                 <property name="password" value="${jdbc.password}" />
22             </dataSource>
23         </environment>
24     </environments>
25     <mappers>
26         <mapper resource="com/zhiyou100/wc/mapper/UsersMapper.xml" />
27     </mappers>
28 </configuration>

Second, the entity class defines aliases for the simplified map references sql xml file

Before we sql mapping xml file when referencing entity class, you need to write the full name of the class entity class (package name + class name), as follows:

1     <insert id="addUser" parameterType="com.zhiyou100.wc.bean.Users" >
2         insert into users(name,age) values(#{name},#{age})
3     </insert>
4 
5     <select id="selectAll" resultType="com.zhiyou100.wc.bean.Users" >
6         select * from users
7     </select>

 

parameterType = "com.zhiyou100.wc.bean.Users" here to write fully qualified class names com.zhiyou100.wc.bean.Users the User entity class , always write such a long list of content very troublesome, and we hope to be able to the following abbreviated form

1     <insert id="addUser" parameterType="_Users" >
2         insert into users(name,age) values(#{name},#{age})
3     </insert>

 

parameterType = "_ Users" so much easier to write, in order to achieve this effect, we need an entity class in conf.xml file = "com.zhiyou100.wc.bean.Users" is defined as an alias "_Users", specific approach is as follows:
  <configuration> </ configuration> tag added conf.xml file follows:

1 <typeAliases>
2     <typeAlias type="com.zhiyou100.wc.bean.Users" alias="_Users"/>
3 </typeAliases>

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/banzhuanlaowang/p/11455220.html