目录
建立空项目
建立一个模块:maven项目
建立数据层dao和业务层service的接口和实现类文件
目录如下:
BookDao.class
package com.itheima.dao;
public interface BookDao {
public void save();
}
BookDaoimpl.class
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {}
private List<String> stringList;
private Map<String, User> userMap;
private Properties properties;
public void setStringList(List<String> stringList) { this.stringList = stringList; }
public void setUserMap(Map<String, User> userMap) { this.userMap = userMap;}
public void setProperties(Properties properties) { this.properties = properties; }
@Override
public void save() {
System.out.println(stringList);
System.out.println(properties);
}
}
BookService.class
package com.itheima.service;
public interface BookService {
public void save();
}
BookServiceimpl.class
package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.dao.impl.BookDaoimpl;
import com.itheima.service.BookService;
public class BookServiceimpl implements BookService {
private BookDao book;
public void save() {
System.out.println("BookService运行了");
book.save();
}
//alt+insert快速生成set方法,再去配置文件中让数据层和业务层有关联
public void setBook(BookDao book) {
this.book = book;
}
}
建立测试类app.class
package com.itheima;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class APPText {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
}
}
在pom.xml导入spring的两个坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springioc05</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</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>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
建立配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 管理Druid数据库池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="mysql://localhost:3306/db_librarysys"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>