<?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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<!-- 数据库连接池 value与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.user}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Mybatis Generator 配置详解 转: https://www.imooc.com/article/21444
mapper.xml
1. /**************************** 商品搜索 ***********************************/
SELECT
<include refid="Base_Column_List" />
from mmall_product
where name like #{productName}
and id = #{productId}
</where>
// 根据productName 或 productId搜索 我们要的是‘或’关系 而不是并
正确的是:
<where> 用where标签无需考虑where后面的条件 它把and替换成where
<if test="productName != null">
and name like #{productName}
</if>
<if test="productId != null">
and id = #{productId}
</if>
</where>
2.
<select id="selectByNameAndProductId" parameterType="map" resultMap="BaseResultMap">
parameterType="map" resultMap="BaseResultMap"是因为
List<Product> productList = productMapper.selectByNameAndProductId(productName,productId);
<?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.mmall.dao.ProductMapper">
<resultMap id="BaseResultMap" type="com.mmall.pojo.Product">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="category_id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="name" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="subtitle" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="main_image" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="sub_images" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="detail" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="price" javaType="java.math.BigDecimal" jdbcType="DECIMAL" />
<arg column="stock" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="status" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
<arg column="update_time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
</constructor>
</resultMap>
<sql id="Base_Column_List">
id, category_id, name, subtitle, main_image, sub_images, detail, price, stock, status,
create_time, update_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from mmall_product
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from mmall_product
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.mmall.pojo.Product">
insert into mmall_product (id, category_id, name,
subtitle, main_image, sub_images,
detail, price, stock,
status, create_time, update_time
)
values (#{id,jdbcType=INTEGER}, #{categoryId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{subtitle,jdbcType=VARCHAR}, #{mainImage,jdbcType=VARCHAR}, #{subImages,jdbcType=VARCHAR},
#{detail,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, #{stock,jdbcType=INTEGER},
#{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
)
</insert>
<insert id="insertSelective" parameterType="com.mmall.pojo.Product">
insert into mmall_product
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="categoryId != null">
category_id,
</if>
<if test="name != null">
name,
</if>
<if test="subtitle != null">
subtitle,
</if>
<if test="mainImage != null">
main_image,
</if>
<if test="subImages != null">
sub_images,
</if>
<if test="detail != null">
detail,
</if>
<if test="price != null">
price,
</if>
<if test="stock != null">
stock,
</if>
<if test="status != null">
status,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="categoryId != null">
#{categoryId,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="subtitle != null">
#{subtitle,jdbcType=VARCHAR},
</if>
<if test="mainImage != null">
#{mainImage,jdbcType=VARCHAR},
</if>
<if test="subImages != null">
#{subImages,jdbcType=VARCHAR},
</if>
<if test="detail != null">
#{detail,jdbcType=VARCHAR},
</if>
<if test="price != null">
#{price,jdbcType=DECIMAL},
</if>
<if test="stock != null">
#{stock,jdbcType=INTEGER},
</if>
<if test="status != null">
#{status,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.Product">
update mmall_product
<set>
<if test="categoryId != null">
category_id = #{categoryId,jdbcType=INTEGER},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="subtitle != null">
subtitle = #{subtitle,jdbcType=VARCHAR},
</if>
<if test="mainImage != null">
main_image = #{mainImage,jdbcType=VARCHAR},
</if>
<if test="subImages != null">
sub_images = #{subImages,jdbcType=VARCHAR},
</if>
<if test="detail != null">
detail = #{detail,jdbcType=VARCHAR},
</if>
<if test="price != null">
price = #{price,jdbcType=DECIMAL},
</if>
<if test="stock != null">
stock = #{stock,jdbcType=INTEGER},
</if>
<if test="status != null">
status = #{status,jdbcType=INTEGER},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.mmall.pojo.Product">
update mmall_product
set category_id = #{categoryId,jdbcType=INTEGER},
name = #{name,jdbcType=VARCHAR},
subtitle = #{subtitle,jdbcType=VARCHAR},
main_image = #{mainImage,jdbcType=VARCHAR},
sub_images = #{subImages,jdbcType=VARCHAR},
detail = #{detail,jdbcType=VARCHAR},
price = #{price,jdbcType=DECIMAL},
stock = #{stock,jdbcType=INTEGER},
status = #{status,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
<resultMap id="BaseResultMap" type="com.mmall.pojo.Product">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="category_id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="name" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="subtitle" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="main_image" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="sub_images" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="detail" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="price" javaType="java.math.BigDecimal" jdbcType="DECIMAL" />
<arg column="stock" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="status" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
<arg column="update_time" javaType="java.util.Date" jdbcType="TIMESTAMP" />
</constructor>
</resultMap>
<sql id="Base_Column_List">
id, category_id, name, subtitle, main_image, sub_images, detail, price, stock, status,
create_time, update_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from mmall_product
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from mmall_product
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.mmall.pojo.Product">
insert into mmall_product (id, category_id, name,
subtitle, main_image, sub_images,
detail, price, stock,
status, create_time, update_time
)
values (#{id,jdbcType=INTEGER}, #{categoryId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{subtitle,jdbcType=VARCHAR}, #{mainImage,jdbcType=VARCHAR}, #{subImages,jdbcType=VARCHAR},
#{detail,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, #{stock,jdbcType=INTEGER},
#{status,jdbcType=INTEGER}, now(), now()
)
</insert>
<insert id="insertSelective" parameterType="com.mmall.pojo.Product">
insert into mmall_product
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="categoryId != null">
category_id,
</if>
<if test="name != null">
name,
</if>
<if test="subtitle != null">
subtitle,
</if>
<if test="mainImage != null">
main_image,
</if>
<if test="subImages != null">
sub_images,
</if>
<if test="detail != null">
detail,
</if>
<if test="price != null">
price,
</if>
<if test="stock != null">
stock,
</if>
<if test="status != null">
status,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="categoryId != null">
#{categoryId,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="subtitle != null">
#{subtitle,jdbcType=VARCHAR},
</if>
<if test="mainImage != null">
#{mainImage,jdbcType=VARCHAR},
</if>
<if test="subImages != null">
#{subImages,jdbcType=VARCHAR},
</if>
<if test="detail != null">
#{detail,jdbcType=VARCHAR},
</if>
<if test="price != null">
#{price,jdbcType=DECIMAL},
</if>
<if test="stock != null">
#{stock,jdbcType=INTEGER},
</if>
<if test="status != null">
#{status,jdbcType=INTEGER},
</if>
<if test="createTime != null">
now(),
</if>
<if test="updateTime != null">
now(),
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.Product">
update mmall_product
<set>
<if test="categoryId != null">
category_id = #{categoryId,jdbcType=INTEGER},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="subtitle != null">
subtitle = #{subtitle,jdbcType=VARCHAR},
</if>
<if test="mainImage != null">
main_image = #{mainImage,jdbcType=VARCHAR},
</if>
<if test="subImages != null">
sub_images = #{subImages,jdbcType=VARCHAR},
</if>
<if test="detail != null">
detail = #{detail,jdbcType=VARCHAR},
</if>
<if test="price != null">
price = #{price,jdbcType=DECIMAL},
</if>
<if test="stock != null">
stock = #{stock,jdbcType=INTEGER},
</if>
<if test="status != null">
status = #{status,jdbcType=INTEGER},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = now(),
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.mmall.pojo.Product">
update mmall_product
set category_id = #{categoryId,jdbcType=INTEGER},
name = #{name,jdbcType=VARCHAR},
subtitle = #{subtitle,jdbcType=VARCHAR},
main_image = #{mainImage,jdbcType=VARCHAR},
sub_images = #{subImages,jdbcType=VARCHAR},
detail = #{detail,jdbcType=VARCHAR},
price = #{price,jdbcType=DECIMAL},
stock = #{stock,jdbcType=INTEGER},
status = #{status,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now()
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectList" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from mmall_product
ORDER BY id asc
</select>
<select id="selectByNameAndProductId" parameterType="map" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from mmall_product
<where>
<if test="productName != null">
and name like #{productName}
</if>
<if test="productId != null">
and id = #{productId}
</if>
</where>
</select>
<select id="selectByNameAndCategoryIds" parameterType="map" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from mmall_product
where status = 1
<if test="productName != null">
and name like #{productName}
</if>
<if test="categoryIdList != null">
and category_id in
<foreach close=")" collection="categoryIdList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
</select>
</mapper>