网上销售平台--spring mybatis springMVC 整合(五)

spring  mybatis的整合

1.配置POM.xml文件 ,配置项目所需jar(见 POM.xml文件)

2.配置WEB.xml:

配置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
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.测试类:
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);
}
}


-----------------------------------------------------------------------------------------------------

springmvc的整合

-----------------------------------------------------------------------------------------------------
1.创建后台配置文件springmvc-back.xml
<context:component-scan base-package="cn.liu"
    use-default-filters="false">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- jsp视图 -->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/back_page/" />
    <property name="suffix" value=".jsp" />
</bean>


2.创建前台配置文件springmvc-front.xml
<context:component-scan base-package="cn.liu"
    use-default-filters="false">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- jsp视图 -->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/back_page/" />
    <property name="suffix" value=".jsp" />
</bean>


3.配置web.xml
<!-- springmvc 后台配置 -->
<servlet>
    <servlet-name>back</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-back.xml</param-value>
    </init-param>

</servlet>
<servlet-mapping>
    <servlet-name>back</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- springmvc 前台配置 -->
<servlet>
    <servlet-name>front</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-front.xml</param-value>
    </init-param>

</servlet>
<servlet-mapping>
    <servlet-name>front</servlet-name>
    <url-pattern>*.shtml</url-pattern>
</servlet-mapping>

<!-- 解决乱码问题 -->
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>

</filter-mapping>

项目结构图(一):




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVCSpringMyBatis是一种常见的JavaWeb开发框架组合,通过整合这三个开源框架,可以方便地进行Web应用的开发。下面是一个简要的整合教程: 1. 创建一个Maven项目,并在pom.xml中添加相应的依赖: - SpringMVC依赖:spring-webmvc、spring-context、spring-beans等。 - Spring依赖:spring-core、spring-context、spring-aop等。 - MyBatis依赖:mybatismybatis-spring、mysql-connector-java等。 2. 创建一个SpringMVC配置文件,如springmvc-servlet.xml,配置SpringMVC的相关信息,并将其配置在web.xml中: ```xml <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 3. 创建一个Spring配置文件,如applicationContext.xml,配置Spring的相关信息: ```xml <context:component-scan base-package="com.example.package" /> <mvc:annotation-driven /> <!-- 配置数据源、事务管理器等 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 数据源相关配置 --> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> ``` 4. 创建一个MyBatis配置文件,如mybatis-config.xml,配置MyBatis的相关信息: ```xml <configuration> <typeAliasesPackage>com.example.package</typeAliasesPackage> <!-- 配置数据源、事务管理器等 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 数据源相关配置 --> </dataSource> </environment> </environments> <!-- 配置映射文件 --> <mappers> <mapper resource="com/example/package/mapper/ExampleMapper.xml"/> </mappers> </configuration> ``` 5. 创建DAO接口和Mapper映射文件,定义数据访问操作。 6. 创建Service类和Controller类,实现业务逻辑和请求处理。 7. 配置数据库连接信息、事务管理、数据访问等相关配置。 通过以上步骤,我们就完成了SpringMVCSpringMyBatis整合。在此基础上,我们可以根据实际需求,进一步开发完善的Web应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值