亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring框架+jdbcTemplate實現(xiàn)增刪改查功能

 更新時間:2021年09月03日 16:06:44   作者:YonChao  
這篇文章主要介紹了Spring框架+jdbcTemplate實現(xiàn)增刪改查功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

SpringMVC架構(gòu)(Model(實體類),Service,Controller層)

Controller(接收參數(shù)調(diào)用業(yè)務(wù)層)–>Service(調(diào)用持久層,處理業(yè)務(wù)邏輯)–>Dao(與數(shù)據(jù)庫交互)

1. IOC(控制反轉(zhuǎn)是一種設(shè)計思想而不是技術(shù))

DI(依賴注入):是IOC思想的一種技術(shù)實現(xiàn)

IOC容器是Spring提供的保存Bean對象的容器

Bean管理操作

1.Xml + 注解

2.javaConfig + 注解

通過xml配置Bean:TODO:

通過javaConfig 配置Bean:TODO:

通過注解配置Bean:TODO:

2. AOP(面向切面)

面向切面的程序設(shè)計思想。橫向的調(diào)用。

eg:一個日志的功能,很多的功能模塊都需要去使用,可以寫一個切面去做這個事情。

使用@Aspect來標(biāo)記一個普通類為切面。

連接點:比如說日志需要作用的方法。

目標(biāo)對象:日志需要使用的對象。

1. 添加依賴

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.3.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.7</version>
    <scope>runtime</scope>
</dependency>

2.demo練習(xí)

需求:SpringIOC + JDBCTemplate實現(xiàn)簡單的數(shù)據(jù)庫操作

1.新建Maven項目并引入Spring核心4依賴

<!--Spring的4個基礎(chǔ)jar包(容器包)-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.1</version>
        </dependency>

jdbc依賴

<!--Spring整合jdbc-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.6</version>
        </dependency>

        <!--mysql驅(qū)動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

junit5

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

2. 創(chuàng)建SpringConfig配置文件(通過JavaConfig方式注入bean)

創(chuàng)建SpringConfig類,添加@Configuration標(biāo)記為配置類。

配置數(shù)據(jù)源和JDBCTemplateBean。

/**
 * @author YonC
 * @date 2021/9/2
 */
@Configuration
public class SpringConfig {
    @Bean
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=ture&charactorEncoding=utf-8&serverTimezone=UTC");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

3.創(chuàng)建MVC架構(gòu)并創(chuàng)建與數(shù)據(jù)庫字段對應(yīng)的實體類對象

實體類:StudentPO

public class StudentPO {
    private Long id;
    private String name;
    private String age;

    public StudentPO() {
    }

    public StudentPO(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public StudentPO(Long id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentPO{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4. 編寫Dao層

面向接口編程,首先定義Dao層的規(guī)范接口,定義了增刪改查4種方法

/**
 * @author YonC
 * @date 2021/9/2
 */
public interface StudentDao {
    void addStudent(StudentPO student);

    void delStudentById(Long id);

    void updateStudent(StudentPO student);

    List<StudentPO> selectStudent();
}

接口的實現(xiàn)

@Repository注解將StudentDao注入IOC容器

@Autowired自動裝配JdbcTemplate對象,JdbcTemplate對象已經(jīng)在SpringConfig文件中實例化

/**
 * @author YonC
 * @date 2021/9/2
 */
@Repository
public class StudentDaoImpl implements StudentDao {


    @Autowired
    JdbcTemplate jdbcTemplate;

    /*
     * 增加Student
     * */
    @Override
    public void addStudent(StudentPO student) {
        jdbcTemplate.update("insert into student (name,age) values (?,?)", student.getName(), student.getAge());
    }

    /*
     * 刪除Student
     * */
    @Override
    public void delStudentById(Long id) {
        jdbcTemplate.update("delete from student where id=?", id);
    }

    /*
     * 修改Student
     * */
    @Override
    public void updateStudent(StudentPO student) {
        String sql = "UPDATE student SET name=?,age=? where id = ? ";
        Object[] args = {student.getName(), student.getAge(), student.getId()};
        jdbcTemplate.update(sql, args);
    }

    /*
     * 查詢
     * */
    @Override
    public List<StudentPO> selectStudent() {
        String sql = "select id,name,age from student";
        return this.jdbcTemplate.query(sql, (rs, index) -> {
            StudentPO student = new StudentPO();
            student.setId(rs.getLong("id"));
            student.setName(rs.getString("name"));
            student.setAge(rs.getString("age"));
            return student;
        });
    }
}

5. Dao與數(shù)據(jù)庫的增刪改查已經(jīng)實現(xiàn),使用Service層去調(diào)用Dao層的方法。

首先定義Service層的接口

/**
 * @author YonC
 * @date 2021/9/2
 */
public interface StudentService {

    void addStudent(StudentPO student);

    void delStudentById(Long id);

    void updateStudent(StudentPO student);

    List<StudentPO> selectStudent();
}

接口實現(xiàn)

@Service將對象聲明IOC容器中

@Autowired自動裝配IOC容器中的StudentDaoStudentDao對象初始化

/**
 * @author YonC
 * @date 2021/9/2
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentDao studentDao;

    @Override
    public void addStudent(StudentPO student) {
        studentDao.addStudent(student);
    }

    @Override
    public void delStudentById(Long id) {
       studentDao.delStudentById(id);
    }

    @Override
    public void updateStudent(StudentPO student) {
       studentDao.updateStudent(student);
    }

    @Override
    public List<StudentPO> selectStudent() {
        return studentDao.selectStudent();
    }
}

6. 使用Junit5單元測試測試

首先通過IOC容器拿到StudentService對象

private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
    // 通過Spring的IOC容器
    private StudentService studentService = applicationContext.getBean(StudentService.class);

測試

/**
 * @author YonC
 * @date 2021/9/2
 */
class StudentServiceImplTest {

    private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
    // 通過Spring的IOC容器
    private StudentService studentService = applicationContext.getBean(StudentService.class);
    @Test
    public void testAddStudent() {

        studentService.addStudent(new StudentPO("zahngsna", "999"));
        System.out.println("添加成功!");
    }

    @Test
    public void testDelStudent() {
        studentService.delStudentById(3L);
        System.out.println("刪除成功!");
    }

    @Test
    public void testUpdateStudent() {
        //將id為3的Student的name修改為"wang",age修改為21
        studentService.updateStudent(new StudentPO(1L,"wang","28"));
        System.out.println("修改成功!");
    }

    @Test
    public void testSelectStudent() {
        studentService.selectStudent().forEach(System.out::println);
    }

}

到此這篇關(guān)于Spring框架+jdbcTemplate實現(xiàn)增刪改查功能的文章就介紹到這了,更多相關(guān)Spring jdbcTemplate增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 五個很實用的IDEA使用技巧分享

    五個很實用的IDEA使用技巧分享

    IntelliJ IDEA 是一款優(yōu)秀的 Java 集成開發(fā)環(huán)境,它提供了許多強大的功能和快捷鍵,可以幫助開發(fā)者提高編碼效率和質(zhì)量,本文就在為你介紹博主常用的五個IntelliJ IDEA使用技巧,希望能夠給你帶來一些工作效率上的提升
    2023-10-10
  • Java?如何接收kernel傳過來的數(shù)組(推薦)

    Java?如何接收kernel傳過來的數(shù)組(推薦)

    這篇文章主要介紹了Java?如何接收kernel傳過來的數(shù)組,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-08-08
  • 通過代碼示例了解submit與execute的區(qū)別

    通過代碼示例了解submit與execute的區(qū)別

    這篇文章主要介紹了通過代碼示例了解submit與execute的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Java?詳解Map集合之HashMap和TreeMap

    Java?詳解Map集合之HashMap和TreeMap

    本章具體介紹了HashMap、TreeMap兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
  • Java中的鎖與鎖的狀態(tài)升級詳細(xì)解讀

    Java中的鎖與鎖的狀態(tài)升級詳細(xì)解讀

    這篇文章主要介紹了Java中的鎖與鎖的狀態(tài)升級詳細(xì)解讀,Java 1.6以后官方針對鎖的優(yōu)化,主要是增加了兩種新的鎖:偏向鎖和輕量級鎖,再加上本身重量級鎖,那么鎖基本上可以大致分為這三種,它們之間的區(qū)別主要是體現(xiàn)在等待時間上面,需要的朋友可以參考下
    2024-01-01
  • SpringBoot中Mockito單元測試入門

    SpringBoot中Mockito單元測試入門

    單元測試在很多地方都用的到,本文主要介紹了SpringBoot中Mockito單元測試入門,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法

    Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • SpringBoot使用MyBatis時的幾種傳參規(guī)范示例

    SpringBoot使用MyBatis時的幾種傳參規(guī)范示例

    使用Mybatis作為持久層框架時,對于數(shù)據(jù)庫的增刪改查等操作都需要參數(shù)的傳遞,本文就詳細(xì)的介紹了一下SpringBoot使用MyBatis時的幾種傳參規(guī)范示例,感興趣的可以了解一下
    2022-02-02
  • SpringBoot集成ip2region實現(xiàn)ip白名單的代碼示例

    SpringBoot集成ip2region實現(xiàn)ip白名單的代碼示例

    ip2region v2.0 - 是一個離線IP地址定位庫和IP定位數(shù)據(jù)管理框架,10微秒級別的查詢效率,提供了眾多主流編程語言的 xdb 數(shù)據(jù)生成和查詢客戶端實現(xiàn),本文介紹了SpringBoot集成ip2region實現(xiàn)ip白名單的代碼工程,需要的朋友可以參考下
    2024-08-08
  • 詳解Java合并數(shù)組的兩種實現(xiàn)方式

    詳解Java合并數(shù)組的兩種實現(xiàn)方式

    這篇文章主要介紹了Java合并數(shù)組的兩種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論