Spring与Dao
程序员文章站
2022-05-07 12:12:01
...
Spring于DAO
Spring与DAO
Spring与JDBC模板(对IOC的应用)
对增删改的操作
//对于增删改操作统一使用update
@Override
//增
public void insertStudent(Student student) {
String sql = "insert into student(id,name,age) values(?,?,?)";
this.getJdbcTemplate().update(sql, student.getId(), student.getName(), student.getAge());
}
//删
@Override
public void deleteById(int id) {
String sql = "delete from student where id=?";
this.getJdbcTemplate().update(sql, id);
}
//改
@Override
public void updateStudent(Student student) {
String sql = "update student set name=?,age=? where id=?";
this.getJdbcTemplate().update(sql, student.getName(), student.getAge(), student.getId());
}
普通Bean的注册
<!--注册Dao-->
<bean id="studentDao" class="priv.ze.spring.Dao.Impl.StudentDaoImpl">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!--注册Service-->
<bean id="studentService" class="priv.ze.spring.Dao.Impl.IStudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean>
数据源的注册
此种方式并不是最简洁的方式
<!--注册数据源 Spring内置连接池-->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?
serverTimezone=GMT%2B8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="00312000"/>
</bean>
<!--注册数据源 c3p0内置连接池-->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?
serverTimezone=GMT%2B8&useSSL=false"/>
<property name="user" value="root"/>
<property name="password" value="00312000"/>
</bean>
<!--注册JdbcTemplate-->
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!--注册Dao-->
<bean id="studentDao" class="priv.ze.spring.Dao.Impl.StudentDaoImpl">
<property name="dataSource" ref="myJdbcTemplate"/>
</bean>
<!--注册Service-->
<bean id="studentService" class="priv.ze.spring.Dao.Impl.IStudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean>
从属性连接读取DB的四要素
<!--注册数据源 c3p0内置连接池-->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--注册属性文件 :方式一-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties"/>
</bean>
<!--注册属性文件 :方式二(常用)-->
<context:property-placeholder location="jdbc.properties"/>
同时需要相应的配置文件(在类路径下)
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8&useSSL=false
jdbc.user=root
jdbc.password=00312000
将数据源注入给Dao
此种方式省略了JdbcTemplate的注册,更简便
<!--注册数据源 c3p0内置连接池-->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8&useSSL=false"/>
<property name="user" value="root"/>
<property name="password" value="00312000"/>
</bean>
<!--注册Dao-->
<bean id="studentDao" class="priv.ze.spring.Dao.Impl.StudentDaoImpl">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!--注册Service-->
<bean id="studentService" class="priv.ze.spring.Dao.Impl.IStudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean>
jdbc模板对象是多例的
jdbcTemplate对象是多例的,即系统会为每一个使用模板对象的线程(方法)创建一个jdbcTemplate实例,并且在该线程(方法)结束时,自动释放jdbcTemplate实例,所以在每次使用jdbcTemplate对象时,搜需要通过getjdbcTemplate()方法获取。
上一篇: 用RestTemplate模拟ajax登录,发送POST请求
下一篇: 阿里云增设开源软件镜像站点