05(maven+SSH)网上商城项目实战之spring mybatis整合

简介:

1.配置POM.xml文件 ,配置项目所需jar
2.配置WEB.xml:
    配置spring监听器

1
2
3
4
5
6
7
8
     < 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 -->
    

1
2
3
4
5
< 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,并配置:
 

1
2
3
4
5
6
7
8
9
10
11
12
    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配置
 

1
2
3
4
5
6
7
8
    < 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文件

1
2
3
4
5
6
7
8
9
10
11
     <!-- 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:    

1
2
3
4
5
6
7
     <!-- 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

1
2
3
4
5
6
7
8
9
10
<? 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



import org.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
@Transactional
public 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.测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.SpringJUnit4ClassRunner;
import  cn.liu.croe.bean.UserInfo;
import  cn.liu.croe.service.IUserInfoService;
 
@RunWith (SpringJUnit4ClassRunner. 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);
     }
}





本文转自 l363130002 51CTO博客,原文链接:http://blog.51cto.com/liuyj/1711849




相关文章
|
29天前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
29天前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
29天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
11 0
|
29天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
11 0
|
28天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
2月前
|
前端开发 Java 数据库连接
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
33 0
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
39 1
|
3月前
|
Java 数据库连接 Maven
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
|
29天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——Dept.java
ssh(Spring+Spring mvc+hibernate)——Dept.java
11 0
|
3月前
|
SQL Java 数据库连接
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
【1月更文挑战第2天】 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投Google Code旗下,iBatis3.x正式更名为MyBatis。代码于2013年11月迁移到Github iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)
197 3
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)

热门文章

最新文章