zoukankan      html  css  js  c++  java
  • 使用映射接口实现数据库的操作

    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">     
    
        <!-- 读取jdbc配置文件 -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <!-- JNDI获取数据源(使用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>
        
        <!-- 事务管理 -->
        <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"/>
        </bean>
        
        <!-- Dao配置 -->
        <bean id="employeeMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="mapperInterface" value="cn.bdqn.dao.EmployeeMapper"/>
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
        
        <!--MapperScannerConfigurer Mybatis-Spring 会自动为我们注册Mapper对应的MapperFactoryBean对象 -->
        <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">   -->
            <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
                  <!-- <property name="basePackage" value="cn.jbit.dao" />  
           </bean> -->
        <!-- service 配置 -->
        <bean id="employeeService" class="cn.bdqn.service.impl.EmployeeServieImpl">
            <property name="employeeMapper" ref="employeeMapper"/>
        </bean>
        <!-- spring可以自动去扫描base-pack下面或者子包下面的java文件,
                    若扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean-->       
        <!--  <context:component-scan base-package="cn.jbit.service" />   -->   
         
        
     </beans>

    EmployeeMapper

    public interface EmployeeMapper {
        
        //实现根据部门查询出该部门下的员工信息
        public List<Employee> getEmployeeListByDepId(Department department);
        
        //实现查询员工详细信息的功能
        public Employee getEmployeeById(Employee employee);
    }

    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">
        //resultMap  为了显示出要展现的(去掉不想显示出来的东西比如密码。。。)column对应到数据库列名
      //type是要显示的类的属性的类 property就是Employee中的属性
      //resultMap不能喝resultType同时使用
    <resultMap type="Employee" id="employeeResult"> <result property="id" column="id"/> <result property="sn" column="sn"/> <result property="name" column="name"/> <result property="depName" column="depName"/> </resultMap> //下面的resultMap引用上面的resultMap <select id="getEmployeeById" parameterType="Employee" resultMap="employeeResult"> select e.*,d.depName as depName from employee e,department d where e.id = #{id} and e.depId = d.id </select> <select id="getEmployeeListByDepId" parameterType="Department" resultMap="employeeResult"> select e.*,d.depName as depName from employee e,department d where e.depId = d.id <if test="id != null"> and e.depId = #{id} </if> </select> </mapper>

    Department .java

    public class Department {
        private Integer id;
        private String depName;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getDepName() {
            return depName;
        }
        public void setDepName(String depName) {
            this.depName = depName;
        }
        
    }
    Employee .java
    public class Employee {
        private Integer id;
        private String sn;
        private String name;
        private String gender;
        private Integer depId;
        private String depName;
        
        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 Integer getDepId() {
            return depId;
        }
        public void setDepId(Integer depId) {
            this.depId = depId;
        }
        public String getDepName() {
            return depName;
        }
        public void setDepName(String depName) {
            this.depName = depName;
        }
    }

    service--EmployeeService.java

    public interface EmployeeService {
        //实现根据部门查询出该部门下的员工信息
        public List<Employee> getEmployeeListByDepId(Department department);
        
        //实现查询员工详细信息的功能
        public Employee getEmployeeById(Employee employee);
    }
    EmployeeServieImpl 
    public class EmployeeServieImpl implements EmployeeService{
        
        @Resource
        private EmployeeMapper employeeMapper;
        
        public EmployeeMapper getEmployeeMapper() {
            return employeeMapper;
        }
    
        public void setEmployeeMapper(EmployeeMapper employeeMapper) {
            this.employeeMapper = employeeMapper;
        }
    
        @Override
        public List<Employee> getEmployeeListByDepId(Department department) {
            // TODO Auto-generated method stub
            return employeeMapper.getEmployeeListByDepId(department);
        }
    
        @Override
        public Employee getEmployeeById(Employee employee) {
            // TODO Auto-generated method stub
            return employeeMapper.getEmployeeById(employee);
        }
    
    }

    测试类

    public class EmployeeServiceTest {
        
        private Logger logger = Logger.getLogger(EmployeeServiceTest.class);
        protected ApplicationContext ctx = null;
        private EmployeeService employeeService;
        @Before
        public void setUp() throws Exception{
            ctx = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
            employeeService = (EmployeeService)ctx.getBean("employeeService");
        }
        
        @Test
        public void getEmployeeListByDepIdTest() {
            
             List<Employee> list = null;
             Department department = new Department();
             department.setId(2);
             list  = employeeService.getEmployeeListByDepId(department);
             if(null != list && list.size() > 0){
                 for(Employee _employee : list){
                     logger.debug("getEmployeeListByDepIdTest---> " + _employee.getName());
                 }
             }else{
                 logger.debug("getEmployeeListByDepIdTest---> 该部门没有员工数据!");
             }
        }
        
        @Test
        public void getEmployeeByIdTest() {
            
            Employee employee = new Employee();
            employee.setId(2);
            Employee _employee = null;
            _employee  = employeeService.getEmployeeById(employee);
            if(null != _employee ){
                logger.debug("getEmployeeByIdTest---> " + _employee.getName());
            }else{
                logger.debug("getEmployeeByIdTest---> 没有员工数据!");
            }
        }
    
    }
  • 相关阅读:
    如何更改 iOS 和安卓浏览器上的 input[type="radio"] 元素的默认样式?
    js获取样式、currentStyle和getComputedStyle的兼容写法
    js选中文字兼容性解决
    最短的IE判断var ie=!-[1,]分析
    总结oninput、onchange与onpropertychange事件的用法和区别
    jQuery工具函数
    Vnpy官网汇总
    Anaconda下各个Python版本下载地址
    APScheduler——定时任务框架
    PyQt5——隐藏控件并保留位置
  • 原文地址:https://www.cnblogs.com/xuerong/p/5000468.html
Copyright © 2011-2022 走看看