• 使用SqlSessionTemplate实现数据库的操作


    EmployeeMapper.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.bdqn.dao.EmployeeMapper">
        
        <select id="getEmployeeList" resultType="Employee">
            select * from Employee
        </select>
        <insert id="addEmployee" parameterType="Employee">
            insert into employee (sn,name,gender) values (#{sn},#{name},#{gender})
        </insert>
        <update id="modifyEmployee" parameterType="Employee">
            update employee 
            <set>
                <if test="sn != null"> sn = #{sn},</if>
                <if test="name != null"> name = #{name},</if>
                <if test="gender != null"> gender = #{gender}</if>
            </set>
            where id = #{id}
        </update>
        <delete id="deleteEmployee" parameterType="Employee">
            delete from employee where id = #{id}
        </delete>
    </mapper>

    Employee.java

    public class Employee {
        private Integer id;
        private String sn;
        private String name;
        private String gender;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getSn() {
            return sn;
        }
        public void setSn(String sn) {
            this.sn = sn;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        
    }

    测试类

    public class EmployeeDaoTest {
        
        
        private SqlSessionTemplate sqlSessionTemplate;
        
        protected ApplicationContext ctx = null;
        private EmployeeDaoTest test;
        
        private Logger logger = Logger.getLogger(EmployeeDaoTest.class);
        
        @Before
        public void setUp() throws Exception{
            ctx = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
            test = (EmployeeDaoTest)ctx.getBean("employeeDaoTest");
        }
        
        @Test
        public void getEmployeeListTest() {
            List<Employee>  list = test.sqlSessionTemplate.selectList("cn.bdqn.dao.EmployeeMapper.getEmployeeList");
            logger.debug("testGetEmployeeList---> " + list.size());    
        }
        
        @Test
        public void addEmployeeTest(){
            Employee employee = new Employee();
            employee.setSn("CESHI");
            employee.setName("测试");
            employee.setGender("男");
            int flag = test.sqlSessionTemplate.insert("cn.bdqn.dao.EmployeeMapper.addEmployee", employee);
            Assert.assertEquals(1, flag);
        }
        
        @Test
        public void modifyEmployeeTest(){
            Employee employee = new Employee();
            employee.setId(8);
            employee.setSn("CESHI888");
            employee.setName("测试888");
            employee.setGender("男");
            int flag = test.sqlSessionTemplate.update("cn.bdqn.dao.EmployeeMapper.modifyEmployee", employee);
            Assert.assertEquals(1, flag);
        }
        
        @Test
        public void deleteEmployeeTest(){
            Employee employee = new Employee();
            employee.setId(8);
            int flag = test.sqlSessionTemplate.delete("cn.bdqn.dao.EmployeeMapper.deleteEmployee", employee);
            Assert.assertEquals(1, flag);
        }
        public SqlSessionTemplate getSqlSessionTemplate() {
            return sqlSessionTemplate;
        }
    
        public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
            this.sqlSessionTemplate = sqlSessionTemplate;
        }
    
    
    }

    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>
           <typeAliases>  
               <!--这里给实体类取别名,方便在mapper配置文件中使用-->  
              <package name="cn.bdqn.pojo"/>
           </typeAliases>  
    </configuration>

    applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">     
                
           <!-- Properties文件读取配置,base的properties -->  
           <context:property-placeholder location="classpath:jdbc.properties"/>   
           
           <!-- JNDI获取数据源(使用dbcp连接池) -->    
           <!-- 因为我们使用的这个数据源是采用 dbcp连接池,对于连接池来说,整个应用中只有一个,
                                  所以作用域需要设置成单例 因为获取数据源是非常消耗性能,所以我们也要采用单例模式-->
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
          scope="singleton">
               <property name="driverClassName" value="${driverClassName}" />  
               <property name="url" value="${url}" />  
               <property name="username" value="${username}" />  
               <property name="password" value="${password}" />  
          </bean>
           
           <!-- 事务配置  在需要事务管理的地方加@Transactional 注解或者AOP进行事务处理-->
           <bean id="transactionManager" 
             class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
           </bean>
            
           <!-- 配置mybitas SqlSessionFactoryBean  -->
           <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
               <property name="dataSource" ref="dataSource" />  
               <property name="configLocation" value="classpath:mybatis-config.xml" />  
               <property name="mapperLocations" value="classpath:cn/bdqn/dao/*.xml"/> 
           </bean>  
           
           
           <!-- 配置SqlSessionTemplate -->
            <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
                <!-- spring使用构造的方法进行注入 -->
                <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
            </bean>
            
                   
            <!-- 把sqlSessionTemplate注入测试类 -->
            <bean id="employeeDaoTest" class="cn.bdqn.test.EmployeeDaoTest">
                <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>  
            </bean>
            
    
    </beans>

    加个日志文件和数据文件就好了。

  • 相关阅读:
    0909 初识编译原理
    校园跳蚤市场-Sprint计划
    校园跳蚤市场
    5.2-5.3
    5.1封装
    阅读2
    汉堡包
    五章-问题
    结对子作业 四则运算 V2.0
    四则运算升级版
  • 原文地址:https://www.cnblogs.com/xuerong/p/5000456.html
Copyright © 2020-2023  润新知