Java Web项目整合Spring3和Mybatis3初步应用

一、导入的JAR包:

       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-beanutils-1.8.3.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-collections-3.2.1.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-dbcp-1.4.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-lang-2.6.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-logging-1.1.1.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-pool-1.5.6.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/ezmorph-1.0.6.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/log4j-1.2.16.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/json-lib-2.4-jdk15.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/slf4j-api-1.6.1.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/slf4j-log4j12-1.6.1.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/mybatis-3.0.6.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/mybatis-spring-1.0.2.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/mysql-connector-java-5.1.18-bin.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.aop-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.asm-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.aspects-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.beans-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.context-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.context.support-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.core-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.expression-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.jdbc-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.transaction-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.web-3.0.6.RELEASE.jar"/>
       <classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.web.servlet-3.0.6.RELEASE.jar"/>
  

二、XML 配置文件

 

    applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!--配置数据源,MySQL数据库,使用DBCP数据库连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!--MySQL数据源驱动类-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!--MySQL数据库连接URL,字符编码UTF-8-->
        <property name="url" value="jdbc:mysql://localhost:3306/mol?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" />
        <!--数据库连接用户和密码-->
        <property name="username" value="dev" />
        <property name="password" value="123456" />
        <!--最大连接数-->
        <property name="maxActive" value="100" />
        <!--空闲最大连接数-->
        <property name="maxIdle" value="30" />
        <!--连接超时时限,毫秒单位-->
        <property name="maxWait" value="30000" />
        <property name="defaultAutoCommit" value="true" />
    </bean>
    
    <!--Mybatis的SqlSessionFactory工厂Bean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--设置Mybatis配置文件-->
        <property name="configLocation" value="classpath:conf/mybatis-config.xml"/>
        <!--设置数据源-->
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!--Mybatis的映射器Mapper工厂Bean-->
    <bean id="regUserDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
         <!--设置映射器接口-->
        <property name="mapperInterface" value="com.leading.mol.client.dao.usermanage.RegisterUserDAO" />
    </bean>
    
    <!--将映射器Bean注入Service类-->
    <bean id="userLoginService" class="com.leading.mol.client.service.usermanage.impl.UserLoginServiceImpl">
        <property name="regUserDAO" ref="regUserDAO" />
    </bean>
</beans>
 

    Spring在web.xml的配置

  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:conf/spring/applicationContext.xml</param-value>
  </context-param>
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 

    Mybatis-Config.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>
    <mappers>
        <mapper resource="com/leading/mol/client/dao/usermanage/mapper/RegisterUser.xml"/>
    </mappers>
</configuration>
 

 

三、Mybatis的Mapper配置文件和接口

 

    Mybatis的Mapper配置文件

    <?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.leading.mol.client.dao.usermanage.RegisterUserDAO">
    <resultMap id="registerUserList" type="com.leading.mol.client.domain.usermanage.RegisterUser" >
        <id property="userId" column="userid"/>
        <result property="username" column="username"/>
        <result property="truename" column="truename"/>
        <result property="password" column="password"/>
        <result property="email" column="email"/>
    </resultMap>
    
    <select id="getUserInfoByAccount" resultMap="registerUserList" parameterType="String">
    select userid, username, truename, password, email
      from mol_register_users 
     where email=#{email}
    </select>
    
    <insert id="insertUser" parameterType="com.leading.mol.client.domain.usermanage.RegisterUser">
    insert into mol_register_users (username, truename, password, email, comment)
    values (#{username}, #{truename}, #{password}, #{email}, #{comment})
    </insert>
</mapper>
 

    Mybatis的Mapper接口

public interface RegisterUserDAO
{
    
    /**
     * 功能描述,根据邮箱查询注册用户
     * @param email
     * @return
     * @throws Exception
     */
    public List<RegisterUser> getUserInfoByAccount(String emailAccout) throws Exception;
    
    /**
     * 功能描述,新增注册用户记录
     */
    public int insertUser(RegisterUser regUser) throws Exception;

}
 

 

四、获取Bean

UserLoginService userLoginService = (UserLoginService) WebApplicationContextUtils
                    .getRequiredWebApplicationContext(
                            request.getSession().getServletContext()).getBean(
                            "userLoginService");
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值