spring+mybatis+springmvc integration

Keywords: Mybatis JDBC Spring xml

dao layer integration of spring and mybatis

Required jar package

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

log4j.properties

# Global logging configuration, DEBUG is recommended for development environment
log4j.rootLogger=DEBUG,stdout
#Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] -%m%n

I. po class generated by reverse engineering, no change recommended

package cn.itcast.ssm.po;
import java.util.Date;
public class Items {
    private Integer id;
    private String name;
    private Float price;
    private String pic;
    private Date createtime;
    private String detail;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
    public Float getPrice() {
        return price;
    }
    public void setPrice(Float price) {
        this.price = price;
    }
    public String getPic() {
        return pic;
    }
    public void setPic(String pic) {
        this.pic = pic == null ? null : pic.trim();
    }
    public Date getCreatetime() {
        return createtime;
    }
    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail == null ? null : detail.trim();
    }
}

2. Wrapper class (continuously nesting po classes in po classes) (extensibility)

package cn.itcast.ssm.po;

public class ItemsQueryVo {
	//Package commodity information
	private Items items;
	//In order to expand the system scalability, the original po is extended
	private ItemsCustomer itemsCustomer;
	public Items getItems() {
		return items;
	}
	public void setItems(Items items) {
		this.items = items;
	}
	public ItemsCustomer getItemsCustomer() {
		return itemsCustomer;
	}
	public void setItemsCustomer(ItemsCustomer itemsCustomer) {
		this.itemsCustomer = itemsCustomer;
	}
}

3. The original po class is generated by reverse engineering. If the database fields are adjusted, only reverse engineering is needed.
Therefore, to facilitate development, we choose to extend po classes (inheritance).

package cn.itcast.ssm.po;
/*Product information extension class */
public class ItemsCustomer extends Items{
	//Add product information extension attribute
}

IV. configure sqlMapConfig.xml

<?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>

<! -- the configuration of global setting is an extremely important adjustment setting in mybatis, which will change the operation and implementation of mybatis
	Parameters: cache, delay loading, column label replacing alias, timeout, automatic hump naming rule mapping
 -->
<!-- <settings></settings> -->

<! -- configure alias 
Role: reduce programmer workload
 For example, items can be used instead of cn.itcast.ssm.po.Items
-->
<typeAliases>
	<! -- batch scan the class under the package, alias is the class name, case insensitive (generally lowercase) - >
	<package name="cn.itcast.ssm.po"/>
</typeAliases>

<! -- configure mapper
 Due to the use of spring and mybatis integration package for mapper scanning, there is no need to configure 
Do not configure but must follow rules:
mapper.xml and mapper.java have the same name and are in the same directory
-->
</configuration>

V. configure applicationContext-dao.xml

<?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
<!--  Load db.properties Contents of the document -->
<context:property-placeholder location="classpath:db.properties"/> 

<!-- Configure data sources ,dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}"></property>
	<property name="url" value="${jdbc.url}"></property>
	<property name="username" value="${jdbc.username}"></property>
	<property name="password" value="${jdbc.password}"></property>
	<property name="maxActive" value="30"></property>
	<property name="maxIdle" value="5"></property>
</bean>

<!-- To configure SqlSessionFactory (Integration package)-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- Database connection pool -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- Load mybatis Global profile for -->
	<property name="configLoaction" value="classpath:mybatis/sqlMapConfig.xml"></property>
</bean>

<!-- To configure mapper Scanner (integration package)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
	<property name="sqlSessionFactoryBean" value="sqlSessionFactory"></property>
</bean>

 </beans>

////mapper.xml and mapper.java must be in the same directory

Vi. mapper.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="cn.itcast.ssm.mapper.ItemsMapperCustomer">


<!-- Define the sql Segment (product query criteria) -->
	<sql id="query_items_where">
	<!-- Usage dynamics sql,adopt if Judge and meet the conditions SQL Splicing -->
	<!-- Product query criteria passed ItemsQueryVo Middle package object itemsCustomer Property transfer -->
	<if test="itemsCustomer!=null">
		<if test="itemsCustomer.name!=null and itemsCustomer.name!=''">
			items.name LIKE '%${itemsCustomer.name}%'
		</if>
	</if>	
	</sql>
	
<!-- 	Commodity list query
	parameterType Incoming wrapper object (wrapped query criteria)
	resultType Extended objects are recommended -->
	<select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo" 
	resultType="cn.itcast.ssm.po.ItemsCustomer">
		SELECT items.* FROM items 
		 <where>
		    <include refid="query_items_where"></include>
		</where>
	</select>
</mapper>

VII. mapper.java

package cn.itcast.ssm.mapper;

import cn.itcast.ssm.po.ItemsCustomer;
import java.util.List;

public interface ItemsMapperCustomer {
    public List<ItemsCustomer> findItemsList();
}

Posted by seidel on Tue, 19 Nov 2019 11:50:43 -0800