欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Idea -Spring5进行数据库的链接

程序员文章站 2022-07-13 10:39:47
...

第一步:

导入对应的jar 包 https://pan.baidu.com/s/1JuR5YXdv2GfL-mGhq0Ii0A 提取码:1234

第二步:

创建相应的properties 文件:
本次链接数据库 采用德鲁伊
Idea -Spring5进行数据库的链接

第三步:

创建xml文件 进行解析

<!-- 读取外部文件  location 后面是自己properties 文件名称 其他的 不需要改-->

 <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!-- 数据库连接池的链接-->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
       destroy-method="close">
  <property name="url" value="${prope.url}" />
  <property name="username" value="${prope.username}" />
  <property name="password" value="${prope.password}" />
  <property name="driverClassName" value="${prope.driverClass}" />
 </bean>

<!-- 配置jdbctemplate 类-->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

第四步

自己创建和数据库对应的 类 以及 Service Dao 等类 即可准备测试

特别注意 dao类和Service 类需要使用注解

dao 类里面有个固定对象:
Idea -Spring5进行数据库的链接

各个类代码如下:
entity 类: 是按照数据库的生成的 每个人 各不一样

package zyc.entity;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/11/2 18:19
 * @version: v1.0
 * @modified By:
 */
public class User {

    private Integer id;
    private String username;
    private String password;
    private  String email;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public User() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Service 类:

package zyc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zyc.dao.BookDao;
import zyc.entity.User;

import java.util.List;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/11/2 18:11
 * @version: v1.0
 * @modified By:
 */
@Service
public class BookService  {
    @Autowired
    private BookDao bookDao;

    public void addUser(User user){
        bookDao.add(user);
    }

    public void  deleteUser(int id){
        bookDao.deleteUser(id);
    }

    public void fixUser(User user){
        bookDao.fixUser( user);
    }

    public Integer selectUsers(){
        return bookDao.selectUsers();
    }

    public User findUser(int id){
        return bookDao.findUser(id);
    }

    public List<User> findUsers(){
       return  bookDao.findUsers();
    }

}

Dao类:

package zyc.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.core.JdbcTemplate;
import zyc.entity.User;

import java.sql.SQLException;
import java.util.List;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/11/2 18:11
 * @version: v1.0
 * @modified By:
 */
@Repository
public class BookDaoImpl implements BookDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(User user) {
        String sql="INSERT INTO USER VALUES(?,?,?,?)";
        jdbcTemplate.update(sql,user.getId(),user.getUsername(),user.getPassword(),user.getEmail());
    }

    @Override
    public void deleteUser(int id) {
        String sql="delete from user where id=?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public void fixUser(User user) {

        String sql="update user set  username=?,password=? WHERE id=?";
        jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getId());
    }

    @Override
    public Integer selectUsers() {
        String sql="select count(*) from user ";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    @Override
    public User findUser(int id) {
        String sql="select * from user where id=?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

    @Override
    public List<User> findUsers() {
        String sql="select * from user";
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return users;
    }
}

第五步:

返回 xml 文件 启用 扫描实现注解:
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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">


 <!-- 数据库连接池 -->

<!-- 读取外部文件-->
 <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!-- 数据库连接池的链接-->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
       destroy-method="close">
  <property name="url" value="${prope.url}" />
  <property name="username" value="${prope.username}" />
  <property name="password" value="${prope.password}" />
  <property name="driverClassName" value="${prope.driverClass}" />
 </bean>

<!-- 配置jdbctemplate 类-->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

<!-- zyc  换成自己对应的项目的包名  我是把所有的类放在 zyc 这个包下面的-->
 <context:component-scan base-package="zyc"></context:component-scan>

</beans>

第六步 :

自己生成测试类做测试!
取一部分:

@Test
    public void addUser() {
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean.xml");//读取
                //配置文件
        BookService bookService =
                context.getBean("bookService",BookService.class);//生成
                //对应的对象
        User user=new User();
        //user  是按照数据库的生成的 每个人 各不一样
        user.setUsername("zyc7");
        user.setPassword("wyy");
        user.setEmail("[email protected]");
        user.setId(null);
        bookService.addUser(user);
    }
相关标签: Spring5 spring