其他分享
首页 > 其他分享> > Spring01

Spring01

作者:互联网

Spring概述

Spring介绍

Spring框架的优势(高内聚 低耦合)

程序的耦合和解耦

程序的耦合

解决程序耦合

SpringIOC机制详解

IOC概述及作用

SpringIOC机制详解

Spring基于XML的IOC细节

手动实现自己的IOC容器

Spring管理bean细节

Spring依赖注入(容器给属性赋值)

依赖注入的介绍

Spring中依赖注入方式

Spring配置文件模块化

Spring模块化介绍

Spring模块化配置

Spring整合JDBC实现用户CRUD

整合思路分析

pojo

/**
 * @author: ChengLong
 * @version: 11.0.2
 * @datetime: 2021/9/16 20:43
 */
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
    private Integer p_id;
    private String p_name;
}

dao

import com.offcn.pojo.Person;

/**
 * @author: ChengLong
 * @version: 11.0.2
 * @datetime: 2021/9/16 16:38
 */
public interface PersonDao {
    public int addPerson(Person person);
}

接口实现

import com.offcn.pojo.Person;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author: ChengLong
 * @version: 11.0.2
 * @datetime: 2021/9/16 16:45
 */
public class PersonDaoImpl implements PersonDao {
    //数据库交互方法必须依赖一个对象 jdbcTemplate
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int addPerson(Person person){
        String sql = "insert into person(p_id,p_name) values(?,?)";
        int updata = jdbcTemplate.update(sql,person.getP_id(),person.getP_name());
        return updata;
    }
}

servicedao

import com.offcn.pojo.Person;

/**
 * @author: ChengLong
 * @version: 11.0.2
 * @datetime: 2021/9/16 20:48
 */
public interface PersonService {
    public boolean savePerson(Person person);
}

service

import com.offcn.dao.PersonDao;
import com.offcn.pojo.Person;

/**
 * @author: ChengLong
 * @version: 11.0.2
 * @datetime: 2021/9/16 20:48
 */
public class PersonServiceImpl implements PersonService {

    private PersonDao personDao;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Override
    public boolean savePerson(Person person) {
        return personDao.addPerson(person)>0;
    }
}

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<!--管理一切java对象-->
    <bean id="service" class="com.offcn.service.PersonServiceImpl">
        <property name="personDao" ref="pdao"></property>
    </bean>
<!--    dao对象-->
    <bean id="pdao" class="com.offcn.dao.PersonDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--        操作数据库的工具类,在操作数据库过程中使用数据库连接-->
        <constructor-arg name="dataSource" ref="ds"></constructor-arg>
    </bean>

<!--    容器管理的数据源对象-->
    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="root"></property>
        <property name="password" value="1s2h3a4o56789"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://192.168.196.181:3306/0622db"></property>
    </bean>
</beans>

测试

import com.offcn.dao.PersonDao;
import com.offcn.pojo.Person;
import com.offcn.service.PersonService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author: ChengLong
 * @version: 11.0.2
 * @datetime: 2021/9/16 20:52
 */
public class SpringJdbc {

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-core.xml");
        PersonDao personDao = (PersonDao) context.getBean("pdao");
        Person person = new Person();
        person.setP_id(12);
        person.setP_name("武松");
        PersonService personService = (PersonService) context.getBean("service");
        boolean b = personService.savePerson(person);
        if(b){

        }else{

        }
    }
}

标签:配置文件,Spring,Spring01,person,bean,import,public
来源: https://www.cnblogs.com/chenglong0201/p/15302442.html