Spring jdbc
代码实例:
1.创建数据库 user 下user1表
2.定义实体类
package cn.zjw.dao.impl;
public class UserInfo {
private int id;
private String name;
private String pwd;
private String realName;
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public UserInfo() {
super();
}
}
2.接口
package cn.zjw.dao;
import cn.zjw.dao.impl.UserInfo;
import java.util.List;
public interface UserInfoDao {
public List<UserInfo> findUserAll();
public void update(UserInfo ui);
}
3.实现类—实现这个方法
package cn.zjw.dao.impl;
import cn.zjw.dao.UserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class UserInfoDaoImpl implements UserInfoDao {
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<UserInfo> findUserAll() {
return jdbcTemplate.query("select * from user1", new RowMapper<UserInfo>() {
@Nullable
@Override
public UserInfo mapRow(ResultSet rs, int num) throws SQLException {
UserInfo ui=new UserInfo();
ui.setId(rs.getInt(1));
ui.setName(rs.getString(2));
ui.setPwd(rs.getString(3));
ui.setRealName(rs.getString(4));
return ui;
}
});
}
@Override
public void update(UserInfo ui) {
jdbcTemplate.update("update user1 set name=? where id=?",ui.getId(),ui.getName(),ui.getPwd(),ui.getRealName());
}
}
4.service
package cn.zjw.Service;
import cn.zjw.dao.UserInfoDao;
import cn.zjw.dao.impl.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserInfoService {
@Autowired
private UserInfoDao userInfoDao;
public UserInfoDao getUserInfoDao() {
return userInfoDao;
}
public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
}
public List<UserInfo> findUserAll(){
return userInfoDao.findUserAll();
}
public void update(UserInfo ui){
userInfoDao.update(ui);
}
}
5.创建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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--自动扫描上下文-->
<context:component-scan base-package="cn.zjw.*"></context:component-scan>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/user?characterEncoding=utf8&useSSL=false&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="tz" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--AOP配置-->
<aop:config>
<aop:pointcut id="point" expression="execution(* cn.zjw.dao.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="tz" pointcut-ref="point"></aop:advisor>
</aop:config>
</beans>
6 .测试类
package cn.zjw.test;
import cn.zjw.Service.UserInfoService;
import cn.zjw.dao.impl.UserInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Text1 {
public static void main(String[] args) {
test1();
}
private static void test1() {
ApplicationContext ac= new ClassPathXmlApplicationContext("Spring.xml");
UserInfoService userInfoService= (UserInfoService) ac.getBean("userInfoService");
List<UserInfo> user=userInfoService.findUserAll();
for (UserInfo u:user ){
System.out.println(u.getPwd());
}
}
}
7.运行结果