美文网首页
JAVA 核心笔记 || [xxx] Spring 之 JDBC

JAVA 核心笔记 || [xxx] Spring 之 JDBC

作者: 魔介 | 来源:发表于2018-12-05 17:37 被阅读0次

Spring JDBC

App.java

import com.mj.dao.UserDao;
import com.mj.model.ModelUser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.FileNotFoundException;

public class App {



    public static void main(String args[]) throws FileNotFoundException{
        /*
        //ClassPathXmlApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/Bean.xml");
        BeanSay sa = (BeanSay) context.getBean("BeanSay");
        sa.setMsg("__Spring");
        sa.talk();

        //FileSystemXmlApplicationContext
        ApplicationContext fileContext = new FileSystemXmlApplicationContext("/src/com/mj/xml/Bean.xml");
        BeanSay sa1 = (BeanSay) fileContext.getBean("BeanSay");
        sa1.setMsg("=Spring====");
        sa1.talk();

        ApplicationContext animalContext = new ClassPathXmlApplicationContext("com/mj/xml/Bean.xml");
        BeanAnimal ani =  (BeanAnimal) animalContext.getBean("Animal");
        ani.setAnimalName("dog");
        ani.showAnimal();

        BeanAnimal animal =  (BeanAnimal) animalContext.getBean("Animal");
        animal.setAnimalName("pig");
        animal.showAnimal();

        // init method   destroy method
        BeanLife life = (BeanLife) context.getBean("BeanLife");
        life.showName();

        //BeanDog 继承  BeanAnimal
        BeanDog dog = (BeanDog)context.getBean("BeanDog");
        dog.showAnimal();
        //加载多配置文件
        BeanUser user = (BeanUser)context.getBean("BeanUser");
        user.showUser();
        */


        /*
        ApplicationContext annotationCtx = new AnnotationConfigApplicationContext(BeanConfig.class);
        IBean beanPerson = (IBean) annotationCtx.getBean("beanPerson");
        beanPerson.show();

        IBean beanStu = (IBean)annotationCtx.getBean("beanStu");
        beanStu.show();

        IBean beanTch = (IBean)annotationCtx.getBean("beanTch");
        beanTch.show();
        */

        /*
        // Setter 注入
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/BeanDL.xml");
        BeanShowHelper hps = (BeanShowHelper) context.getBean("BeanShowHelper");
        hps.show();

        //构造函数注入
        BeanHideHelper bhh = (BeanHideHelper) context.getBean("BeanHideHelper");
        bhh.show();
        */

        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/jdbc/BeanJdbc.xml");
        UserDao ud = (UserDao) context.getBean("UserDao");
        ModelUser modelU = new ModelUser();
        modelU.setName("mjjjjjj");
        ud.insert(modelU);

        ModelUser rsU = ud.findByID(4);
        System.out.println( rsU.getName());



    }
}


UserDao.jva

package com.mj.dao;

import com.mj.dao.IUserDao;
import com.mj.model.ModelUser;

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

public class UserDao {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    public void insert(ModelUser u){

        String sql = "INSERT INTO user"+" (name)"+"  VALUES(?)";

        Connection conn = null;

        try{
            conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,u.getName());
            ps.executeUpdate();
            ps.close();

        }catch(SQLException e){
            throw new RuntimeException(e);

        }


    }


    public ModelUser findByID(int id){

        String sql = "SELECT * FROM user Where id=?";

        try{
            Connection conn = dataSource.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);

            ResultSet rs = ps.executeQuery();
            ModelUser u = null;
            if(rs.next()){
                u = new ModelUser();
                u.setName( rs.getString("name"));
                u.setId( rs.getInt("ID"));
            }

            rs.close();
            ps.close();
            return u;


        }catch(SQLException e){
            throw  new RuntimeException( e );
        }

    }


}

ModelUser.java

package com.mj.model;

public class ModelUser {
    private int id;
    private  String name;

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

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

BeanJdbc.xml

<!--
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <import resource="Spring-datasource.xml" />
    <import resource="UserDao.xml" />

</beans>
-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- mysql bean -->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/Person" />
        <property name="username" value="root" />
        <property name="password" value="654321" />
    </bean>

    <bean id="UserDao" class="com.mj.dao.UserDao" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>

</beans>

运行

[mysql:][db:Person]>select * from user;
+----+---------+
| id | name    |
+----+---------+
|  1 | jgg     |
|  2 | MJ      |
|  3 | mjjjjjj |
|  4 | mjjjjjj |
+----+---------+
4 rows in set (0.00 sec)

相关文章

网友评论

      本文标题:JAVA 核心笔记 || [xxx] Spring 之 JDBC

      本文链接:https://www.haomeiwen.com/subject/anpwcqtx.html