1 通过Maven的形式导入用到的JAR包坐标
<dependencies>
<!--通过Maven传递依赖,导入依赖jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
2 查看MySQL的表结构
3 根据表结构编写pojo层
package com.zxf.pojo;
import java.util.Date;
public class MyIp {
private int id;
private String ip_address;
private Date ip_date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIp_address() {
return ip_address;
}
public void setIp_address(String ip_address) {
this.ip_address = ip_address;
}
public Date getIp_date() {
return ip_date;
}
public void setIp_date(Date ip_date) {
this.ip_date = ip_date;
}
}
4 编写DAO层
package com.zxf.dao;
import com.zxf.pojo.MyIp;
import java.util.List;
public interface MyIpDao {
public List<MyIp> getAll();
}
package com.zxf.dao.impl;
import com.zxf.dao.MyIpDao;
import com.zxf.pojo.MyIp;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class MyIpDaoImpl implements MyIpDao {
private DataSource datasource;
public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}
public List<MyIp> getAll() {
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
List<MyIp> list=new ArrayList<MyIp>();
try{
conn=datasource.getConnection();
pstm=conn.prepareStatement("select * from myip");
rs=pstm.executeQuery();
while (rs.next()){
MyIp myIp=new MyIp();
myIp.setId(rs.getInt("id"));
myIp.setIp_address(rs.getString("ip_address"));
myIp.setIp_date(rs.getTimestamp("ip_date"));
list.add(myIp);
}
}catch (Exception e){
e.getMessage();
}
return list;
}
}
5 编写Service层
package com.zxf.service;
import com.zxf.pojo.MyIp;
import java.util.List;
public interface MyIpService {
public List<MyIp> getAll_IP();
}
package com.zxf.service.impl;
import com.zxf.dao.MyIpDao;
import com.zxf.pojo.MyIp;
import com.zxf.service.MyIpService;
import java.util.List;
public class MyIpServiceImpl implements MyIpService {
private MyIpDao myIpDao;
public void setMyIpDao(MyIpDao myIpDao) {
this.myIpDao = myIpDao;
}
public List<MyIp> getAll_IP() {
return myIpDao.getAll();
}
}
6 编写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">
<!-- 配置连接池对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.148:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="abc123"/>
</bean>
<!-- service-->
<bean id="myipservice" class="com.zxf.service.impl.MyIpServiceImpl">
<property name="myIpDao" ref="myipdao"/>
</bean>
<!-- dao-->
<bean id="myipdao" class="com.zxf.dao.impl.MyIpDaoImpl">
<property name="datasource" ref="dataSource"/>
</bean>
</beans>
7 编写测试类
@Test
public void test3(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
MyIpServiceImpl myipservice = ac.getBean("myipservice", MyIpServiceImpl.class);
List<MyIp> all_ip = myipservice.getAll_IP();
for(MyIp myIp:all_ip){
System.out.print(myIp.getIp_address()+" ");
System.out.println(myIp.getIp_date());
}
}
8 运行结果