MyBatis ----- 3. Optimize and solve different entity class attributes and field names conflict

1.MyBatis optimization

1.1 the connection configuration information in the database in a single file properties

  Db.properties create a file, add content:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=123456

  In conf.xml configuration file:

<properties resource="db.properties"/>   <!-- 存放文件路径-->
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC" />
    
    <dataSource type="POOLED">
        <property name="driver" value="${driver}" />    <!-- 使用${属性} -->
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </dataSource>

1.2 alias is defined as an entity class, the use of labels typeAliases

  type: the entity class path alias: alias to be played

<typeAliases>
  <typeAlias type="com.zhiyou.zyl.bean.Users" alias="_Users"/>
</typeAliases>

Add 1.3 log jar package, terms of finding errors

  Add Log4j jar package: log4j-1.2.16.jar

  Create a log4j.properties file:

log4j.properties,
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

  The results look like this:

 

 

2. Solve the field names and attribute names are not the same entity class conflict

  When you create the entity class table, you need to take the initiative to set up the field name of the entity class attributes and data tables, like, but sometimes inconsistent field names and the entity class attributes, mybatis the property will not be resolved, it is necessary to deal with this contradictory.

  Tables and test data 

CREATE TABLE orders(
order_id INT PRIMARY KEY AUTO_INCREMENT,
order_no VARCHAR(20),
order_price FLOAT);
INSERT INTO orders(order_no, order_price) VALUES('aaaa', 23);
INSERT INTO orders(order_no, order_price) VALUES('bbbb', 33);
INSERT INTO orders(order_no, order_price) VALUES('cccc', 22);

  Entity classes:

public class Order {
  private int id;
  private String no;
  private float price;
}

  Two ways to resolve the conflict:

  The first: In the SQL statement from the query field alias

<select id="getOrder" parameterType="int" resultType="com.zhiyou.zyl.bean.Orders">
  select order_id id, order_no no,order_price price from orders where order_id=#{id}
</select>

  II: Use <resultMap> tag

  < SELECT ID = "getOrder" The resultMap = "Order" > <-! ResulyMap = "The resultMap ID tag name" -> 
        SELECT * WHERE Orders from order_id ID = # {} 
  </ SELECT >   < The resultMap type = "COM. zhiyou.zyl.bean.Orders " ID =" Order " > < ID column =" order_id " Property =" ID " /> <-! ID primary key, result in other fields, column field name, property of the entity class properties -> < Result column = "order_no" property = "NO" /> <result column="order_price" property="price"/> </resultMap>

 

Guess you like

Origin www.cnblogs.com/zyl187110/p/11442180.html