项目结构图:
[dangerbox title=”整合内容”]
1.spring抢MyBatis的连接
2.Spring抢Mapper接口
3.spring抢Mapper.xml文件
[/dangerbox]
1.导入MyBatis框架。
导入三个jar包:
mybatis-3.2.2.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.18-bin.jar
2.myeclipse导入Spring框架。
3.老规矩,编写实体类。
[sourcecode language=”java” title=”com.domain.Goods.java”]
package com.domain;
public class Goods {
private Integer goodsId;
private String goodsName;
private Double goodsPrice;
private Integer goodsNum;
private Integer goodsType;
public Goods() {
}
public Goods(Integer goodsId, String goodsName, Double goodsPrice,
Integer goodsNum, Integer goodsType) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNum = goodsNum;
this.goodsType = goodsType;
}
public Goods(String goodsName, Double goodsPrice,
Integer goodsNum, Integer goodsType) {
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNum = goodsNum;
this.goodsType = goodsType;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Double getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Double goodsPrice) {
this.goodsPrice = goodsPrice;
}
public Integer getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(Integer goodsNum) {
this.goodsNum = goodsNum;
}
public Integer getGoodsType() {
return goodsType;
}
public void setGoodsType(Integer goodsType) {
this.goodsType = goodsType;
}
@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
+ ", goodsPrice=" + goodsPrice + ", goodsNum=" + goodsNum
+ ", goodsType=" + goodsType + "]";
}
}
[/sourcecode]
4.配置spring环境文件(重要)!
[sourcecode language=”xml” title=”applicationContext.xml”]
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!– 设置Spring注册 –>
<context:annotation-config/>
<!– Spring扫描路径 –>
<context:component-scan base-package="com.service"/>
<!– 引入jdbc配置文件 –>
<context:property-placeholder location="classpath:jdbc.properties" />
<!–创建jdbc数据源 –>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="url" value="${db.url}" />
</bean>
<!– 配置MyBatis的sqlSession –>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<!– 映射Mapper目录 –>
<!– Mapper接口所在包名,Spring会自动查找其下的Mapper –>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper" />
</bean>
<!– 可通过注解控制事务 –>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
[/sourcecode]
[sourcecode language=”xml” title=”jdbc.properties”]
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc\:mysql\://127.0.0.1\:3306/spring?useUnicode\=true&characterEncoding\=utf8
db.username=root
db.password=123456
[/sourcecode]
[sourcecode language=”xml” title=”mybatis.xml”]
<?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>
</configuration>
[/sourcecode]
5.编写mapper接口。
[sourcecode language=”java” title=”com.mapper.GoodsMapper.java”]
package com.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.domain.Goods;
@Repository("GoodsMapper")
public interface GoodsMapper {
public List<Goods> findAll();
public void save(Goods goods);
}
[/sourcecode]
[sourcecode language=”xml” title=”com.mapper.GoodsMapper.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.mapper.GoodsMapper">
<select id="findAll" resultMap="resultGoods">
select * from goods
</select>
<insert id="save" parameterType="com.domain.Goods">
insert into goods(goods_id,goods_name,goods_price,goods_num,goods_type)
value(#{goodsId},#{goodsName},#{goodsPrice},#{goodsNum},#{goodsType})
</insert>
<resultMap type="com.domain.Goods" id="resultGoods">
<id property="goodsId" column="goods_id" javaType="int" />
<result property="goodsName" column="goods_name" javaType="java.lang.String" />
<result property="goodsPrice" column="goods_price" javaType="double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</mapper>
[/sourcecode]
6.编写业务层 GoodsService。
[sourcecode language=”java” title=”com.service.GoodsService.java”]
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.domain.Goods;
import com.mapper.GoodsMapper;
@Service("GoodsService")
public class GoodsService {
@Autowired
@Qualifier("GoodsMapper")
private GoodsMapper goodsMapper;
public GoodsMapper getGoodsMapper() {
return goodsMapper;
}
public void setGoodsMapper(GoodsMapper goodsMapper) {
this.goodsMapper = goodsMapper;
}
//readOnly只读,不需要回滚
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<Goods> findAll(){
try{
return this.goodsMapper.findAll();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
//rollbackFor 如果错误就回滚
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
public void save(Goods goods){
try{
this.goodsMapper.save(goods);
System.out.println("数据插入成功!");
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
[/sourcecode]
7.编写测试类。
有一个测试注释掉了,两个是有冲突的。
[sourcecode language=”java” title=”com.test.test.java”]
package com.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.domain.Goods;
import com.service.GoodsService;
public class test {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context
= new ClassPathXmlApplicationContext("/applicationContext.xml");
GoodsService goodsService = (GoodsService) context.getBean("GoodsService");
// List<Goods> goodsList = goodsService.findAll();
// for (Goods goods : goodsList) {
// System.out.println(goods);
// }
goodsService.save(new Goods("桌球",1d,5,1));
}
}
[/sourcecode]
mybatis-3.2.2.jar:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/jar包/mybatis-3.2.2.jar[/bdbtn]
mybatis-spring-1.1.1.jar:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/jar包/mybatis-spring-1.1.1.jar[/bdbtn]
mysql-connector-java-5.1.18-bin.jar:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/jar包/mysql-connector-java-5.1.18-bin.jar[/bdbtn]
项目源代码:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/源代码/MB010.rar[/bdbtn]