spring mybatis整合

1.配置POM.xml文件 ,配置項目所需jar

2.配置WEB.xml:

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
     
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

配置spring監聽器

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.創建anotation.xml配置文件

<!-- spring掃描 @service -->
<context:component-scan base-package="cn.liu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:annotation-config/>

4. 創建jdbc.properties和jdbc.xml,並配置:

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8
user=root
password=
<!-- c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}" />
<property name="password" value="${password}"/>
</bean>

5.配置property.xml文件,讀取JDBC配置

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- JDBC的配置 -->
<value>classpath:properties/jdbc.properties</value>
</list>
</property>
 </bean>

6.配置mybatis.xml文件


<!-- mybatis org.mybatis.spring.SqlSessionFactoryBean 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/liu/croe/dao/*.xml"/>
<property name="typeAliasesPackage" value="cn.liu.croe.bean"></property>
</bean>
<!-- 掃包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.liu.croe.dao"/>
</bean>

7.事務管理transation.xml:

<!-- spring 事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 開啓事務註解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

8.UserInfoDAO.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="cn.liu.croe.dao.UserInfoDAO">
     
    <insert id="add" parameterType="UserInfo">
        insert into user_info (user_name,user_sex)
        values(#{userName},#{userSex})
    </insert>
     
</mapper>

9.事務管理註釋:@Transactional

importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import cn.liu.croe.bean.UserInfo;import cn.liu.croe.dao.UserInfoDAO;import cn.liu.croe.service.IUserInfoService;
@Service@Transactionalpublic class UserInfoServiceImpl implements IUserInfoService {    @Autowired    private UserInfoDAO userdao;        public void addUser(UserInfo user) {                int i = userdao.add(user);        System.out.println(i);                throw new RuntimeException("運行時異常");            }}

10.測試類:

package cn.shop.userinfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import cn.liu.croe.bean.UserInfo;
import cn.liu.croe.service.IUserInfoService;
 
@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
public class UserTest {
    @Autowired
    private IUserInfoService service;
    @Test
    public void testAdd() {
        UserInfo user = new UserInfo();
        user.setUserName("秋香");
        user.setUserSex("女");
        service.addUser(user);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章