Comparison and solution of JDBC and mybatis

1. Frequent database link creation and release cause waste of system resources and thus affect system performance. This problem can be solved if the database link pool is used.

 Solution: Configure the data link pool in SqlMapConfig.xml and use the connection pool to manage database links. dataSource type="POOLED"

<environments default="mysql">   
<!-- 配置 mysql 的环境 -->   
    <environment id="mysql">    
        <!-- 配置事务的类型 -->    
        <transactionManager type="JDBC"></transactionManager>    
        <!-- 配置连接数据库的信息:用的是数据源(连接池) -->    
        <dataSource type="POOLED">    
             <property name="driver" value="com.mysql.jdbc.Driver"/>   
             <property name="url" value="jdbc:mysql://localhost:3306/ee50"/>    
             <property name="username" value="root"/>    
             <property name="password" value="1234"/>   
         </dataSource>   
    </environment>  
</environments>   

2. Sql statement written in the code makes the code difficult to maintain , and the actual application of sql may change greatly, and the change of sql requires changing the java code.

Solution: Separate the Sql statement configuration from the java code in the xxxDao.xml file.

3. It is troublesome to pass parameters to the sql statement , because the where condition of the sql statement is not necessarily, there may be more or less, and the placeholders need to correspond to the parameters.

Solution: Mybatis automatically maps java objects to sql statements, and defines the type of input parameters through the parameterType in the statement .

4. It is troublesome to parse the result set . SQL changes cause the parsing code to change, and it needs to be traversed before parsing. It is more convenient if the database records can be encapsulated into pojo objects for parsing. Solution: Mybatis automatically maps the sql execution result to the java object, and defines the type of the output result through the resultType in the statement

 

Guess you like

Origin blog.csdn.net/weixin_43725517/article/details/108859441