Spring-Jdbc的配置和注解

来源:互联网 发布:分屏软件下载 编辑:程序博客网 时间:2024/06/06 05:11

一.xml配置方式:

先导包:c3p0-0.9 1.2.jar包,

                commons-logging-1.1.3jar,

               mysql-connector-java-5.1.38

  bean : 属性id  class scope(singletone\prototype)
property : name就是bean的成员变量名(提供getter和setter方法)  value/ref (ref指的是引用另一个bean)


<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
       <context:component-scan base-package="com.lrq"/>
       <bean id="employee" class="com.lrq.entity.Employee">
       </bean>


      创建数据库连接池

       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
       <property name="user" value="root"></property>
       <property name="password" value="12345"></property>
       <!--连接池启动的时候默认创建的连接数量-->
       <property name="initialPoolSize" value="3"></property>
       <!--连接池最多可以管理的连接对象个数-->
       <property name="maxPoolSize" value="100"></property>
       <!--连接池中最多能够管理的statement对象-->
       <property name="maxStatements" value="1000"></property>
       <!--一旦连接池中现有的连接数量不够,每次增长的连接数目:5  ,但是连接池中的连接数量-->
       <!--最多不可超过maxPoolSize中设置的连接数目-->
       <property name="acquireIncrement" value="5"></property>
       </bean>
      配jdbcTemplate


       <!--2、创建jdbcTemplate:对jdbc的操作进行了封装-->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
              <property name="dataSource" ref="dataSource"/>
       </bean>
       配Dao层
       <!--3、创建dao类-->
       <bean id="employeeDao" class="com.lrq.dao.daoImpl.EmployeeDAOImpl">
              <property name="jdbcTemplate" ref="jdbcTemplate"/>
       </bean>


       <bean id="empDao" class="com.lrq.dao.daoImpl.EmpDaoImpl">
              <property name="jdbcTemplate" ref="jdbcTemplate"/>
       </bean>
</beans>    


实体类:
package com.lrq.entity;


import org.springframework.stereotype.Component;


/**
 * Created by Administrator on 2017/3/16.
 */
@Component("person")
public class Person {
    private int id;
    private String name;
    public Person() {
    }


    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }


    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;
    }


    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}




dao层:
public interface EmpDao {
    public Emp getEmpById(int id);
}


daoImpl层:
@Repository/*("empDao")*/
public class EmpDaoImpl implements EmpDao{
   /* @Resource(name="jdbcTemplate")*/
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public Emp getEmpById(int id) {
        String sql="select emp.*,dept.* from emp,dept where emp.dept_id=dept.deptId and emp.empId=?";
        return jdbcTemplate.queryForObject(sql,new myResult(),id);


    }


    class myResult implements RowMapper<Emp> {
        @Override
        public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
            Emp emp=new Emp();
            emp.setEmpId(resultSet.getInt("empId"));
            emp.setEmpName(resultSet.getString("empName"));
            emp.setSalary(resultSet.getDouble("salary"));


            Dept dept=new Dept();
            dept.setDeptId(resultSet.getInt("deptId"));
            dept.setDeptName(resultSet.getString("deptName"));


            emp.setDept(dept);
            return emp;
        }
    }
}
测试类:
public class TestEmp {
    public static void main(String[] args) {
        ApplicationContext apc=new ClassPathXmlApplicationContext("spring_jdbc.xml");
        EmpDao empDao= (EmpDao) apc.getBean("empDao");
        Emp emp=empDao.getEmpById(1);
        System.out.println(emp.getDept()+":"+emp.getEmpId()+":"+emp.getEmpName()+":"+emp.getSalary());
        System.out.println(emp.getDept().getDeptId()+":"+emp.getDept().getDeptName());
    }
}




结果:
com.lrq.entity.Dept@bef2d72:1:qreq:7000.0
1:设计部


二.注解配置方式:
在声明注解之前要先导入Spring的Aop包
spring-aop-4.3.7.Release
然后配置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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
       <context:component-scan base-package="com.lrq"/>
<beans>
如红色字体所示,为注解方式
@Component("person")
@Repository("empDao")
@Resource(name="jdbcTemplate")
注解:
在spring中启用扫描注解:component-scan  base-package:扫描的包路径
@Component-----实体类层等
@Repository(beanid)--daoImpl层
@Service----serviceImpl层
@Controller--控制层
依赖注入:
@Resource写在成员变量名的上面,name属性指向bean id


三.xml配置+注解自动注入:
类上面的注解(后面不跟名字,全在xml声明)
@Component
@Repository
@Resource


属性上面的注解
@Autowired
xml配置相关属性
一般不提倡使用自动注入,如果出错无法判断在哪出的问题


四.xml每次获取同一对象:
配置是否单例
@Scope(“singletone|prototype”)
单例下次我会细讲
常见异常:
1.如果不通过Bean产生对象,直接new对象,测试时发现空指针异常;相当于你new 的对象没有注入进去
2.property name="jdbcTemplate" 飘红,一般是没设定set方法



                                             
1 0
原创粉丝点击