Spring+Mybatis

基于之前的spring项目,引入mybatis作为数据层基本框架 1. 引入mybatis依赖

3.3.0 1.2.3 org.mybatis mybatis-spring ${mybatis-spring.version} org.mybatis mybatis ${mybatis.version}

2. 创建Domain类 2.1 表结构

CREATE TABLE `stock` (
`code` varchar(10) NOT NULL,
`name` varchar(10) NOT NULL,
`count` int(11) DEFAULT NULL,
`main_business` varchar(256) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

2.2 Stock.java

package com.zmannotes.spring.mybatis.domain;

public class Stock {

private String code;

private String name;

private Integer count;

private String mainBusiness;

private Date createTime;

private BigDecimal price;

//setter and getter
//…
}

3. 创建Dao(Mapper)

package com.zmannotes.spring.mybatis.dao;

import com.zmannotes.spring.mybatis.domain.Stock;

public interface StockMapper {
//根据PK删除
int deleteByPrimaryKey(String code);
//插入新纪录
int insert(Stock record);
//根据PK查询
Stock selectByPrimaryKey(String code);
}

4. 配置映射文件

//通过配置文件将 数据库表 与 对象、Dao关联起来












code, name, count, main_business, create_time, price



delete from stock
where code = #{code,jdbcType=VARCHAR}


insert into stock (code, name, count,
main_business, create_time, price
)
values (#{code,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{count,jdbcType=INTEGER},
#{mainBusiness,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{price,jdbcType=DECIMAL}
)

5. 配置数据源及SessionFactory










set names utf8mb4;


<!-- 事务管理器 -->
<bean id="stock\_txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="stock\_dataSource" />
</bean>

<tx:annotation-driven transaction-manager="stock\_txManager"
    mode="proxy" proxy-target-class="true" />

<!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory -->
<!-- 创建sqlsessionFactory 并指定数据源 -->
<bean id="stock\_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="stock\_dataSource" />
    <property name="typeAliasesPackage"
        value="com.zmannotes.spring.mybatis.domain" />
    <!-- 配置扫描Mapper XML的位置 -->
    <property name="mapperLocations">
        <list>
            <value>classpath:mybatis/stock/\*.xml</value>
        </list>
    </property>
</bean>

<!-- 使用MapperScannerConfiguer 扫描来实现 -->
<!-- 这里指定了要扫描的映射接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage">
        <value>
            com.zmannotes.spring.mybatis.dao
        </value>
    </property>
    <property name="sqlSessionFactoryBeanName" value="stock\_sqlSessionFactory" />
</bean>

6. 完成单测

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = “/spring/stock-session-factory.xml”)
public class StockTest {
@Autowired
private StockMapper stockMapper;

/\*\*插入一条数据并自动回滚\*/
@Transactional
@Test
public void test(){
    Stock stock = new Stock();
    stock.setCode("900001");
    stock.setName("tcl");
    stockMapper.insert(stock);
}

}

源码Github Q&A Q:无聊的24步有没有快捷完成办法? A:当然有!那就是Mybatis Generator,自动生成24所有代码。参考 LINK