PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-02-27 21:26:29  修改时间:2024-10-30 10:55:21  分类:06.Java框架/系统  编辑

pom.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd
">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springinaction</groupId>
    <artifactId>wen-jianbao</artifactId>
    <packaging>jar</packaging>
    <version>3.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--<springframework.version>4.3.30.RELEASE</springframework.version>-->
        <springframework.version>3.2.18.RELEASE</springframework.version>
    </properties>

    <dependencies>

        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!-- Spring依赖 -->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->

        <!-- 1.Spring核心依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- 2.AOP依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- 3.Spring dao依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <!-- MySQL Driver -->
        <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

    </dependencies>
</project>

src/main/resources/datasource.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
">

    <!-- 开启Bean注解自动 装配/检测 功能 -->
    <context:component-scan base-package="com.habuma.spitter"/>

    <!-- 配置数据源 -->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/spring_in_action?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8&amp;useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
</beans>

JAVA代码

1.Spitter.java

package com.habuma.spitter.persistence.model;

public class Spitter {
    private int id;
    private String username;
    private String password;
    private String fullname;
    private String email;
    private int update_by_email;

    public Spitter() {
    }

    public int getId() {
        return id;
    }

    public void setId(int 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 getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmail() {
        return email;
    }

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

    public int getUpdate_by_email() {
        return update_by_email;
    }

    public void setUpdate_by_email(int update_by_email) {
        this.update_by_email = update_by_email;
    }
}

2.SpitterService.java

package com.habuma.spitter.persistence;

import com.habuma.spitter.persistence.model.Spitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Component
public class SpitterService {
    private static final String SQL_INSERT_SPITTER = "INSERT INTO spitter(username,password,fullname) VALUES (?,?,?)";
    @Autowired
    private DataSource dataSource;

    /**
     * 添加用户
     */
    public void addSpitter(Spitter spitter) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = dataSource.getConnection();
            stmt = conn.prepareStatement(SQL_INSERT_SPITTER);

            stmt.setString(1, spitter.getUsername());
            stmt.setString(2, spitter.getPassword());
            stmt.setString(3, spitter.getFullname());

            stmt.execute();
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

3.Test.java

import com.habuma.spitter.persistence.SpitterService;
import com.habuma.spitter.persistence.model.Spitter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("datasource.xml");
        SpitterService spitterServ = (SpitterService) ctx.getBean("spitterService");

        Spitter spitter= new Spitter();
        spitter.setUsername("建宝");
        spitter.setPassword("123456");
        spitter.setFullname("温建宝");
        spitter.setEmail("phpjava8642@163.com");
        spitter.setUpdate_by_email(2);
        spitterServ.addSpitter(spitter);
        
        System.out.println("添加成功");
    }
}