我的博客名為黑客之謎,喜歡我的,或者喜歡未來的大神,點一波關注吧!順便說一下,雙十二快到了,祝大家雙十二快樂,盡情的買買買~
如果轉載我的文章請標明出處和著名,謝謝配合。
我的博客地址為: https://www.cnblogs.com/themysteryofhackers/p/12021646.html
更新時間為:2019-12-11
一、創建Maven項目
如何創建Maven項目的步驟我就不再論述了,忘記的話就看會我上一篇博客吧!
二、導入項目的依賴
導入的依賴如下:
<properties> <spring.version>4.3.12.RELEASE</spring.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- spring aop支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- aspectj支持 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- 添加MySQL數據庫驅動依賴包. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- druid 連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 代碼檢錯版本號 --> <source>1.8</source> <!-- 代碼編譯版本號 --> <target>1.8</target> <!--項目字符集 --> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
三、創建包和類
導入依賴之后,就創建包、類和配置文件,具體文件如下圖:
User.java
package com.zzx.entity; import java.util.Date; public class User { private Integer id; private String name; private String password; private String email; private Integer age; private Integer sex; private Date birth; private Integer state; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", age=" + age + ", sex=" + sex + ", birth=" + birth + ", state=" + state + '}'; } }
UserDao.java
package com.zzx.dao; import com.zzx.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; //將該對象交給spring容器管理 @Repository("userDao") public class UserDao { @Autowired private JdbcTemplate jdbcTemplate; public List<User> selectAll(){ String sql="select * from user"; return jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class)); } }
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 掃描包 --> <context:component-scan base-package="com.zzx.dao" /> <!-- 引入屬性文件,數據配置信息文件,引入后就可以通過EL表達式獲取該文件的內容 --> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${spring.datasource.driver-class-name}" /> <property name="url" value="${spring.datasource.url}" /> <property name="username" value="${spring.datasource.username}" /> <property name="password" value="${spring.datasource.password}" /> </bean> <!-- 這是spring提供的一個操作數據庫的工具類 ,以為它要用到dataSource,所以要在屬性上配上dataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
jdbc.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useunicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123456
TestJDBC.java
package com.zzx.jdbc; import com.zzx.dao.UserDao; import com.zzx.entity.User; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class TestJDBC { @Test public void testJDBC(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = applicationContext.getBean("userDao", UserDao.class); List<User> users = userDao.selectAll(); for (User user : users) { System.out.println(user); } } @Test public void testDruid(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml"); UserDao userDao = applicationContext.getBean("userDao", UserDao.class); List<User> users = userDao.selectAll(); for (User user : users) { System.out.println(user); } } }
四、運行程序
先運行TestJDBC.java中的testJDBC()方法,結果如下:
五、整合Druid連接池
整合Druid連接池先要導入Druid的依賴,上面到依賴的時候就已經導入了,然后將applicationContext.xml文件里面的內容換成下面的內容即可,然后在測試類中,在寫一個測試的方法(上面已經給出了),
applicationContext2.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 掃描包 --> <context:component-scan base-package="com.zzx.dao" /> <!-- 引入屬性文件,數據配置信息文件,引入后就可以通過EL表達式獲取該文件的內容 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 指定數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="driverClassName" value="${spring.datasource.driver-class-name}" /> <property name="url" value="${spring.datasource.url}" /> <property name="username" value="${spring.datasource.username}" /> <property name="password" value="${spring.datasource.password}" /> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> <!-- 配置初始化大小、最小、最大 --> <property name="maxActive" value="20" /> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,並且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxOpenPreparedStatements" value="20" /> </bean> <!-- 這是spring提供的一個操作數據庫的工具類 ,以為它要用到dataSource,所以要在屬性上配上dataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
運行testDruid方法,結果是和上面使用jdbc運行的一樣的,但使用了連接池好處多多,比如可以讓資源重復利用,不用每次訪問數據庫的時候都要創建一個連接,訪問結束就銷毀這個鏈接,這樣就可以看出使用連接池還可以提高系統的反應速度,畢竟創建連接和銷毀鏈接都是要時間的。如果還想了解連接池的好處,自己可以去學習一下。
結尾
我是一個Java程序員,一個向往技術的小白,以后我會陸續將自己學習到的Java或者其他的知識會以博客的形式分享出來,希望能對大家有幫助。
喜歡小編的就給我一個關注吧!
如果有哪些問題、有哪些需要糾錯或者侵犯到您的權益的地方,可以聯系我,我進行修改。