本文记录springmvc整合dao的配置
整合dao
首先在resource文件夹下添加两个文件:数据库配置文件和日志配置文件
1 2 3 4
| jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://120.25.162.238:3306/mybatis001?characterEncoding=utf-8 jdbc.username=root jdbc.password=123
|
1 2 3 4 5 6
| # Global logging configuration 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
|
sqlMapConfig.xml
mybatis自己的配置文件
在resources目录下新建mybatis文件夹,并新建sqlMapConfig.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?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.iot.learnssm.firstssm.po"/> </typeAliases>
</configuration>
|
applicationContext-dao.xml
在resources目录下新建spring文件夹,并新建applicationContext-dao.xml文件
配置:
- 数据源
- SqlSessionFactory
- mapper扫描器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="30" /> <property name="maxIdle" value="5" /> </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.iot.learnssm.firstssm.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
|
逆向工程生成po类及mapper(单表增删改查)
方法参见《MyBatis学习笔记(18)-mybatis逆向工程》
手动定义商品查询mapper
针对综合查询mapper,一般情况会有关联查询,建议自定义mapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <?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.iot.learnssm.firstssm.mapper.ItemsMapperCustom" >
<sql id="query_items_where"> <if test="itemsCustom!=null"> <if test="itemsCustom.name!=null and itemsCustom.name!=''"> items.name LIKE '%${itemsCustom.name}%' </if> </if>
</sql>
<select id="findItemsList" parameterType="com.iot.learnssm.firstssm.po.ItemsQueryVo" resultType="com.iot.learnssm.firstssm.po.ItemsCustom"> SELECT items.* FROM items <where> <include refid="query_items_where"></include> </where> </select>
</mapper>
|
1 2 3 4
| public interface ItemsMapperCustom { List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception; }
|
1 2 3 4 5 6 7 8 9
| package com.iot.learnssm.firstssm.po;
public class ItemsCustom extends Items{ }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.iot.learnssm.firstssm.po;
public class ItemsQueryVo {
private Items items;
private ItemsCustom itemsCustom;
public Items getItems() { return items; }
public void setItems(Items items) { this.items = items; }
public ItemsCustom getItemsCustom() { return itemsCustom; }
public void setItemsCustom(ItemsCustom itemsCustom) { this.itemsCustom = itemsCustom; } }
|
整合好dao后的工程目录如图