MyBatis学习系列十:Spring集成

来源:互联网 发布:星巢网络2 编辑:程序博客网 时间:2024/05/09 01:57

通过MyBatis Generator 工具自动生成
1.实体

public class Product {    private Integer id;    private String spuCode;    private String name;    private String model;    private String property;    private String remark;}

2.mapper

@Repository("productMapper")public interface ProductMapper {    int deleteByPrimaryKey(Integer id);    int insert(Product record);    Product selectByPrimaryKey(Integer id);    List<Product> selectAll();    int updateByPrimaryKey(Product record);}

3.mapping.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="com.xiang.mapper.ProductMapper">    <resultMap id="BaseResultMap" type="com.xiang.model.Product">        <id column="id" property="id" jdbcType="INTEGER" />        <result column="spuCode" property="spuCode" jdbcType="VARCHAR" />        <result column="name" property="name" jdbcType="VARCHAR" />        <result column="model" property="model" jdbcType="VARCHAR" />        <result column="property" property="property" jdbcType="VARCHAR" />        <result column="remark" property="remark" jdbcType="VARCHAR" />    </resultMap>    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">        delete from product where id = #{id,jdbcType=INTEGER}    </delete>    <insert id="insert" parameterType="com.xiang.model.Product">        insert into product (id, spuCode, name,model, property, remark)        values (#{id,jdbcType=INTEGER}, #{spuCode,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},        #{model,jdbcType=VARCHAR}, #{property,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR})    </insert>    <update id="updateByPrimaryKey" parameterType="com.xiang.model.Product">        update product set spuCode = #{spuCode,jdbcType=VARCHAR},name = #{name,jdbcType=VARCHAR},model = #{model,jdbcType=VARCHAR},        property = #{property,jdbcType=VARCHAR},remark = #{remark,jdbcType=VARCHAR}         where id = #{id,jdbcType=INTEGER}    </update>    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">        select id, spuCode, name, model, property, remark from product where id = #{id,jdbcType=INTEGER}    </select>    <select id="selectAll" resultMap="BaseResultMap">        select id, spuCode, name, model, property, remark from product    </select></mapper>

4.spring配置文件

<?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:p="http://www.springframework.org/schema/p"    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-3.1.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 扫描Spring自动注解 -->    <context:component-scan base-package="com.xiang"></context:component-scan>    <!-- 加载数据库配置属性文件 -->    <context:property-placeholder location="classpath*:db.properties" />    <!-- 数据源连接池管理 -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName">            <value>${db.driverClassName}</value>        </property>        <property name="url">            <value>${db.url}</value>        </property>        <property name="username">            <value>${db.username}</value>        </property>        <property name="password">            <value>${db.password}</value>        </property>    </bean>    <!-- MyBatis -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="mapperLocations" value="classpath:com/xiang/mapping/*.xml"></property>    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.xiang.mapper" />        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>    <!-- 事务控制 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 启用事务自动注解 -->    <tx:annotation-driven transaction-manager="transactionManager" /></beans>

5.目录结构
这里写图片描述

0 0