标签: springmvc mybatisjava
[TOC]mysql
本文记录springmvc整合dao的配置git
首先在resource文件夹下添加两个文件:数据库配置文件和日志配置文件github
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
# 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
mybatis本身的配置文件spring
在resources目录下新建mybatis文件夹,并新建sqlMapConfig.xml文件sql
<?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> <!-- 全局setting配置,根据须要添加 --> <!-- 配置别名 --> <typeAliases> <!-- 批量扫描别名 --> <package name="com.iot.learnssm.firstssm.po"/> </typeAliases> <!-- 配置mapper 因为使用spring和mybatis的整合包进行mapper扫描,这里不须要配置了。 必须遵循:mapper.xml和mapper.java文件同名且在一个目录 --> <!-- <mappers> </mappers> --> </configuration>
在resources目录下新建spring文件夹,并新建applicationContext-dao.xml文件数据库
配置:apache
<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"> <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有必定的特殊规则 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置数据源 ,dbcp --> <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> <!-- 从整合包里找,org.mybatis:mybatis-spring:1.2.4 --> <!-- sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库链接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <!-- mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径,若是须要扫描多个包,中间使用半角逗号隔开 --> <property name="basePackage" value="com.iot.learnssm.firstssm.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 会致使数据源配置无论用,数据库链接不上。 且spring 4弃用 --> </bean> </beans>
方法参见《mybatis学习笔记(18)-mybatis逆向工程》spring-mvc
针对综合查询mapper,通常状况会有关联查询,建议自定义mappermybatis
<?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片断,就是商品查询条件 --> <sql id="query_items_where"> <!-- 使用动态sql,经过if判断,知足条件进行sql拼接 --> <!-- 商品查询条件经过ItemsQueryVo包装对象 中itemsCustom属性传递 --> <if test="itemsCustom!=null"> <if test="itemsCustom.name!=null and itemsCustom.name!=''"> items.name LIKE '%${itemsCustom.name}%' </if> </if> </sql> <!-- 商品列表查询 --> <!-- parameterType传入包装对象(包装了查询条件) resultType建议使用扩展对象 --> <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>
public interface ItemsMapperCustom { //商品查询列表 List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception; }
ItemsCustom
package com.iot.learnssm.firstssm.po; /** * Created by Brian on 2016/3/2. * 商品信息的扩展类 */ public class ItemsCustom extends Items{ //添加商品信息的扩展属性 }
package com.iot.learnssm.firstssm.po; /** * Created by Brian on 2016/3/2. */ public class ItemsQueryVo { //商品信息 private Items items; //为了系统 可扩展性,对原始生成的po进行扩展 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后的工程目录如图