Spring Jdbc浅谈

1.配置

  在maven文件src/main/resources位置上配置一个applicationContext-jdbc.xml文件用来做配置。

  <1>JdbcTemplate的配置

      即配置dataSource装入连接池数据作为jdbcTemplate的属性

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  <property name="driverClassName" value="${mysql_driver}"></property>
  <property name="url" value="${mysql_url}"></property>
  <property name="username" value="${mysql_username}"></property>
  <property name="password" value="${mysql_passwd}"></property>
 </bean>  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

  

  <2>连接池数据的配置(即数据库的账户密码等需要用到的数据)

mysql_driver=com.mysql.cj.jdbc.Driver
mysql_url=jdbc:mysql://localhost:3306/shujukukkkkk?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
mysql_username=root
mysql_passwd=225510

 

  <3>将类自动扫描装配,同时将连接池配置在properties的位置加载

 

//将连接池用到的数据位置写入
<context:property-placeholder location="config/DB.properties"/> //将需要自动扫描类的包名写入
<context:component-scan base-package="com.zzxtit.springboot.jdbc"></context:component-scan>

 

2.具体实现springjdbc

  <1>创立user  数据类 以及userdao 数据操作类

Spring Jdbc浅谈

 

     SysUser的建立(对应数据库中的数据  以及get set方法)

package com.zzxtit.springboot.jdbc;
public class SysUser {
 
 private String userName;
 
 private String passwd;
 
 private String salt;
 
 private String realName;
 
 private String avatar;
   private String phone;
 
 private String email;
 
 private int gender;  private Integer userId;
 
 public Integer getUserId() {
  return userId;
 }
 @Override
 public String toString() {
  return "SysUser [userName=" + userName + ", passwd=" + passwd + ", salt=" + salt + ", realName=" + realName
    + ", avatar=" + avatar + ", phone=" + phone + ", email=" + email + ", gender=" + gender + ", userId="
    + userId + "]";
 }  public void setUserId(Integer userId) {
  this.userId = userId;
 }  public String getUserName() {
  return userName;
 }  public void setUserName(String userName) {
  this.userName = userName;
 }  public String getPasswd() {
  return passwd;
 }  public void setPasswd(String passwd) {
  this.passwd = passwd;
 }  public String getSalt() {
  return salt;
 }  public void setSalt(String salt) {
  this.salt = salt;
 }  public String getRealName() {
  return realName;
 }  public void setRealName(String realName) {
  this.realName = realName;
 }  public String getAvatar() {
  return avatar;
 }  public void setAvatar(String avatar) {
  this.avatar = avatar;
 }  public String getPhone() {
  return phone;
 }  public void setPhone(String phone) {
  this.phone = phone;
 }  public String getEmail() {
  return email;
 }  public void setEmail(String email) {
  this.email = email;
 }  public int getGender() {
  return gender;
 }  public void setGender(int gender) {
  this.gender = gender;
 }
  }

      userdao的实现

        包括jdbaTemplate增删改查  以及通过namedJdbcTemplate的添加

package com.zzxtit.springboot.jdbc;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate npjTemplate;
    //通过id添加数据
    public void insertUserInfo(SysUser su) {
        
        String sql = "insert into t_sys_user (user_name, passwd, salt, real_name, avatar, phone, "
                + "email, gender, create_time) values (?, ?, ?, ?, ?, ?, ?, ?, now())";
        jdbcTemplate.update(sql, su.getUserName(), su.getPasswd(), su.getSalt(), su.getRealName(), 
                su.getAvatar(), su.getPhone(), su.getEmail(), su.getGender());
    }
    
  //通过id查找数据
    public SysUser getUserById(int userId) {
        
        String sql = "select * from t_sys_user where user_id = ?";
        return jdbcTemplate.query(sql, new Object[] {userId}, new BeanPropertyRowMapper<SysUser>(SysUser.class)).get(0);
    }
  //通过id删除数据 public void deleteById(int userId) { String sql="delete from t_sys_user where user_id = ?"; jdbcTemplate.update(sql, userId); }
  //通过id修改数据 public void rebuiltById(SysUser su,int userId) { String sql = "update t_sys_user set user_name = ?, passwd = ?, salt = ? , real_name = ?, avatar = ?, phone = ?, " + "email = ?, gender = ?, create_time = now() where user_id = ?"; jdbcTemplate.update(sql, su.getUserName(), su.getPasswd(), su.getSalt(), su.getRealName(), su.getAvatar(), su.getPhone(), su.getEmail(), su.getGender(),userId); } //通过namedJdbcTemplate添加数据(即利用map对象,不用使用?) public void insertByNJP(SysUser su) { String sql = "insert into t_sys_user (user_name, passwd, salt, real_name, avatar, phone, " + "email, gender, create_time) values (:useName, :passwd, :salt, :realName, :avatar, :phone,:email,:gender, now())"; Map<String,Object> paramMap = new HashMap<String,Object>(); paramMap.put("useName", su.getUserName()); paramMap.put("passwd", su.getPasswd()); paramMap.put("salt", su.getSalt()); paramMap.put("realName", su.getRealName()); paramMap.put("avatar", su.getAvatar()); paramMap.put("phone", su.getPhone()); paramMap.put("email", su.getEmail()); paramMap.put("gender", su.getGender()); npjTemplate.update(sql,paramMap); } }

 

 

上一篇:ubuntu sudo 不需要输入密码


下一篇:ansible补充