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
- MyBatis学习系列十:Spring集成
- Spring Boot系列教程十:Spring boot集成MyBatis
- Mybatis学习笔记-Spring集成Mybatis
- Spring Boot系列教程七:Spring boot集成MyBatis
- mybatis学习笔记03--与spring集成
- Mybatis学习笔记五:集成Spring
- spring boot 集成 mybatis 入门级学习
- 【Mybatis学习总结九】Spring中集成Mybatis
- Spring Boot系列(十) 持久层框架--Mybatis
- jee、spring、spring mvc、mybatis 学习(十)
- mybatis注解+spring集成
- Spring集成MyBatis
- Spring集成Mybatis
- mybatis和spring集成
- spring 集成 mybatis
- Spring集成Mybatis
- spring 集成mybatis
- spring 集成mybatis
- tesseract-OCR字符识别引擎使用入门
- java多线程4--死锁
- 【Android常识】Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才
- 关于Csdn水区被占领一事 (2015-08-01)
- java web系统架构设计需要解决的几个问题
- MyBatis学习系列十:Spring集成
- leetcode--Count Complete Tree Nodes
- codeforces 505 D Mr. Kitayuta's Technology
- tomcat之jvm分析
- python 的基本语法
- 读书笔记-《人人都是产品经理》,重读
- 在eclipse中使用Lombok
- Java设计模式:工厂模式举例
- HDU 1205.吃糖果【鸽巢原理】【8月1】