烂笔头

扬帆起航,再出发!!!

0%

Spring基于注解的CRUD

注解+XML方式

1.在pom.xml中导入相应jar包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

2、在resources目录下创建bean.xml配置文件,并指定Spring在创建容器时要扫描的包,及将项目中依赖的外部jar包中的类及其依赖注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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">

<!--告诉spring在创建容器时要扫描的包-->
<context:component-scan base-package="io.github.lonelyMrZhang"></context:component-scan>

<!--配置QueryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>

<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://192.168.31.254:3306/coupling_code"></property>
<property name="user" value="root"></property>
<property name="password" value="2486"></property>
</bean>
</beans>

3、在对应的类或方法上标记相应的注解

  • 用于创建对象的使用@Component、@Controller、@Service、@Repository中对应的注解
  • 用于注入数据的使用@Autowired[@Qualifier]、@Resource、@Value中对应的注解
  • 用于改变作用范围的@Scope
  • 和生命周期相关 @PreDestroy、@PostConstruct

各种注解的作用详解

项目GitHub地址

全注解方式

完全去除XML文件需要用到的其他注解

  • @Configuration
    • 作用:指定当前类是一个配置类
    • 细节:当配置类作AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。在@ComponentScan({“io.github.lonelyMrZhang”,”config”})(见下一个)注解中指定扫描其他配置类时,在其他配置类上应该标上Configuration注解,Spring只有看到Configuration注解才会将这个类当作配置类
  • @ComponentScan
    • 作用:用于通过注解指定Spring在创建容器时要扫描的包
    • 属性:
      • value:它和basePackages的作用相同,都是用于指定创建容器时要扫描的包。使用此注解就等于在xml配置文件中配置了:<context:component-scan base-package="io.github.lonelyMrZhang"></context:component-scan>
  • @Bean
    • 作用:用于把当前方法的返回值作为bean对象注入Spring的IOC容器中
    • 属性:
      • name:用于指定bean的id,默认为当情方法名称
    • 细节:
      • 当我们使用注解配置方法时,如果方法有参数,Spring框架会去容器中查找有没有可用的bean对象,查找的方式和Autowired注解的作用时一样的
  • @Import:
    • 作用:用于导入其他的配置类
    • 属性:
      • value:用于指定其他配置类的字节码
    • 说明:当我们使用import的注解之后,有import注解的类是父配置类,导入的时子配置类
  • @PropertySource
    • 作用:用于指定properties文件的位置
    • 属性:
      • value:指定文件的名称和路径
      • 关键字:classpath,类路径下

在注解+XML的项目基础上,进行以下操作:

1.删除resources下的bean.xml类,并创建jdbcConfig.properties数据库配置文件

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.31.254:3306/coupling_code
jdbc.user=root
jdbc.password=2486

properties文件中的数据可以使用@Value注解通过EL表达式取出

1
2
@Value("${jdbc.driver}")
private String driver;

2.创建config包下创建SpringConfiguration配置类,并标注@Configuration注解告诉Spring容器这是一个配置类、标注@ComponentScan(“io.github.lonelyMrZhang”)注解告诉Spring创建容器时要扫描的包、标注@PropertySource(“classpath:jdbcConfig.properties”)注解加载properties配置文件,如果还有其他配置类可以使用@Import(JdbcConfiguration.class)注解包含进来,不过要在该类上标注@Configuration注解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

/**
* @description: 配置类,作用和bean.xml一样
* @author: lonely.mr.zhang
* @date: 2020/6/16 11:59 上午
*/
//@Configuration
@Configuration
//@ComponentScan({"io.github.lonelyMrZhang","config"})
@ComponentScan("io.github.lonelyMrZhang")
@Import(JdbcConfiguration.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {

}

项目GitHub地址

您的支持就是我更新的动力!有钱的捧个钱场,没钱的捧个人场,谢谢大家!