当前位置: 首页 > news >正文

东莞一站式网站推广运营湖北省建设安全管理站网站

东莞一站式网站推广运营,湖北省建设安全管理站网站,网络营销价格策略有哪些,项目营销推广方案详细可以参考:朱要光写的SpringMVCMyBatis开发从入门到实践的第7章,其次在http://www.broadview.com.cn/book/5367也可以下载到本书所有的源代码和其中还包括lib包。下列程序在此基础上稍加修改。 工具:myeclipse10 框架版本:my…

详细可以参考:朱要光写的SpringMVC+MyBatis开发从入门到实践的第7章,其次在http://www.broadview.com.cn/book/5367也可以下载到本书所有的源代码和其中还包括lib包。下列程序在此基础上稍加修改。

工具:myeclipse10

框架版本:mybatis3+spring4

数据库:mysql5.5,现在test空间中建立一张名为student的数据库表,主要属性如下:

1、新建一个webproject,其主要程序目录主要如下所示:

说明:dao包主要放数据库查询处理的类(数据交互层),mapper是代理接口,po是和数据库表相映射的java实体类,test是测试类:主要测试mybatis+spring整理时,使用代理和非代理的情况。先讲述不使用代理的情况。

2、主要配置文件:首先在根目录新建一个源文件夹config(注意选择source folder),里面分别放置mybatis、spring、sqlmap的配置文件,具体看图片中的名称。其次,放置两个properties文件,分别是数据库属性和日志属性,是作用于全局的。

3、导入所需的jar包,一般可以直接放入lib文件夹,然后右键build path选择add to build path即可。所需的jar会在后续上传。

4、spring配置文件applicationContext.xml主要配置项如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.2.xsd   
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据源,使用dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- sqlSessinFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybatis的配置文件 -->
        <property name="configLocation" value="mybatis/SqlMapConfig.xml" />
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 原始Dao接口 -->
    <bean id="userDao" class="cn.com.sm.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔 -->
        <property name="basePackage" value="cn.com.sm.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>  

5、mybatis核心配置文件SqlMapConfig.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>
    <!-- settings-->
    <settings>
        <!-- 打开延迟加载的开关 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 将积极加载改为消极加载(及按需加载) -->
        <setting name="aggressiveLazyLoading" value="false"/>
        <!-- 打开全局缓存开关(二级缓存)默认值就是true -->
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
    <!-- 别名定义 -->
    <typeAliases>
        <package name="cn.com.sm.po"/>
    </typeAliases>
    
    <!-- 加载映射文件 -->
    <mappers>
        <!-- 通过resource方法一次加载一个映射文件 -->
        <mapper resource="sqlmap/UserMapper.xml"/>
        <!-- 批量加载mapper
        <package name="cn.com.sm.mapper"/>-->  
    </mappers>
</configuration>

6、UserMapper.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="test">

    <select id="selectUserById" parameterType="int" resultType="cn.com.sm.po.Student">
        SELECT * FROM student WHERE id = #{id}
    </select>
    <select id="selectUsers" resultType="cn.com.sm.po.Student">
        SELECT * FROM student
    </select>
     <insert id="insertUsers" parameterType="cn.com.sm.po.Student">
        insert into student(id, name, age, sex) value (#{id}, #{name}, #{age}, #{sex})
    </insert>

</mapper>

Java实体类Student:

package cn.com.sm.po;
import java.io.*;

public class Student implements Serializable{
    private int id;
    private String name;
    private int age;
    private String sex;
    //无需构造方法
    //public Student(){}
    /*public Student(int id, String name, int age, String sex){
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }*/
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
 

7、db.properties和log4j.properties内容如下:

(1)db.properties

jdbc.driver=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

(2)log4j.properties

# Global logging configuration  
log4j.rootLogger=DEBUG, stdout  
# Console output...  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n 

8、DAO层代码:

(1)数据库操作接口类

package cn.com.sm.dao;

import cn.com.sm.po.Student;

public interface UserDao {
    public Student selectUserById(int id) throws Exception;
    public Student insertUsers(Student student) throws Exception;
}
(2)实现类

package cn.com.sm.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.com.sm.po.Student;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    @Override
    public Student selectUserById(int id) throws Exception {
        // 继承SqlSessionDaoSupport类,通过this.getSqlSession()得到sqlsession
        //SqlSessionDaoSupport类在MyBatis与Spring整合jar包中,其中包含了sqlSessionFactory对象作为成员变量,方便spring注入。
        SqlSession sqlSession = this.getSqlSession();
        Student student = sqlSession.selectOne("test.selectUserById", 1);
        return student;
    }

//其他操作方法可以继续写,这边暂时空

    @Override
    public Student insertUsers(Student student) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}
注意:必须在spring配置文件applicationContext.xml中添加userDao配置,并将上文中定义的sqlSessionFactory当做参数注入,这样继承SqlSessionDaoSupport才有用。SqlSessionDaoSupport是mybatis和spring整合jar中的,包含了sqlSessionFactory作为成员变量,对外提供getter和setter以方便spring注入。

    <!-- 原始Dao接口 -->
    <bean id="userDao" class="cn.com.sm.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

9、编写测试类UserServiceTest类:测试整合后的效果

package cn.com.sm.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.sm.dao.UserDao;
import cn.com.sm.po.Student;

public class UserServiceTest {
    private ApplicationContext applicationContext;  
    
    //在执行测试方法之前首先获Spring配置文件对象
    //注解Before是在执行本类所有测试方法之前先调用这个方法  
    @Before  
    public void setup() throws Exception{  
        applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");  
    }  
      
    @Test  
    public void testFindUserById() throws Exception{  
        //通过配置资源对象获取userDao对象
        UserDao userDao=(UserDao)applicationContext.getBean("userDao");  
        //调用UserDao的方法  
        Student stu=userDao.selectUserById(1);  
        //输出用户信息  
        System.out.println(stu.getId()+":"+stu.getName());  
    }  
}  

运行方式:

效果:输出id和姓名。

 

以上是不使用代理的情况,如果要使用代理则需做以下修改:

1、在applicationContext.xml中添加mapper扫描器,用于指定包下扫描定义的mapper代理接口:

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔 -->
        <property name="basePackage" value="cn.com.sm.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

2、在mapper包下建立代理接口和对应的mapper.xml配置文件:

(1)UserQueryMapper接口

package cn.com.sm.mapper;

import cn.com.sm.po.Student;

/*使用Mapper代理要點:
 * 1、新建一個接口,接口名称与mapper.xml文件名相同
 * 2、接口中定义的方法名、参数、返回类型与mapper.xml中sql映射的id、输入类型和返回类型分别对应相同
 */
public interface UserQueryMapper {
    //根据Id查询用户信息  
      public Student selectUserById(int id) throws Exception;  
}
(2)UserQueryMapper.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.com.sm.mapper.UserQueryMapper">
    <select id="selectUserById" parameterType="int" resultType="cn.com.sm.po.Student">
        SELECT * FROM student WHERE id = #{id}
    </select>
</mapper>  

3、编写测试类:UserMapperTest

package cn.com.sm.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.sm.mapper.UserQueryMapper;
import cn.com.sm.po.Student;

public class UserMapperTest {
    private ApplicationContext applicationContext;  
    
    //在执行测试方法之前首先获Spring配置文件对象
    //注解Before是在执行本类所有测试方法之前先调用这个方法  
    @Before  
    public void setup() throws Exception{  
        applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");  
    }  
      
    @Test  
    public void testFindUserById() throws Exception{  
        //通过配置资源对象获取userDao对象
        UserQueryMapper queryMapper=(UserQueryMapper)applicationContext.getBean("userQueryMapper");  
        //调用UserDao的方法  
        Student stu=queryMapper.selectUserById(2);  
        //输出用户信息  
        System.out.println(stu.getId()+":"+stu.getName());  
    }  
}  

两种整合的方式就讲到这里。

http://www.yayakq.cn/news/579670/

相关文章:

  • 什么网站做宣传好大连 商城网站制作公司
  • 巢湖自助建站系统什么网站可以做调查
  • 中国科技成就ppt宁波seo关键词如何优化
  • 广州做贷款有什么网站vue 做pc网站可以吗
  • 找人做jsp网站iis网站跳转
  • 手机网站开发语言选择福建省建设干部培训中心网站首页
  • 昆明 五华 网站建设重庆人才网
  • 便宜自适应网站建设厂家一般使用的分辨率是多少
  • cpa网站怎么做本网站正在建设图片
  • 临沂做企业网站北京平面设计公司排名
  • 动物做logo的网站wordpress修改文章字体颜色
  • 网站建设原则五大原则合肥网站建设教程
  • 西安自助建站做网站响应式企业网站源码
  • 寒亭区住房和城乡建设局网站手册设计网站
  • 网站开发三端指哪三端免费商城网站源码
  • 响应式网站实例青岛新闻最新消息
  • 南城网站优化公司乌海学校网站建设
  • 有哪些做产品产业链分析的网站网站建设试题卷
  • 凡科网建立网站后怎么修改个人网页需要什么内容
  • 句容网站定制中山软件开发公司
  • 学校官方网站网页设计深圳住房建设网站
  • 玛迪做网站网页制作简单教程
  • 上海网站建设的价格是多少钱中国最大的电商平台是哪家
  • 部队网站制作专业的单位网站开发开发
  • 哪个网站可以做电视背景墙企业网站 合同
  • iis 子网站福州关键词搜索排名
  • 大棚建设的网站网络策划
  • 深圳建设企业网站公司给公司做网站多少钱
  • 重庆网站建设公司的网站怎样能有个人网站
  • 网站开发技术项目式教程淘宝开店铺网站怎么做