使用全XML(掌握)
最关键的bean.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: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的容器管理 -->
<bean id="accountDao" class="com.itheima.daoimpl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="accountService" class="com.itheima.serviceimpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 第三方的资源交给spring的容器管理,c3p0,druid,jdbcTemplate-->
<!-- 让spring加载jdbc.properties文件
标签:context 用来引入外部的properties文件;用到el表达式 ${key}
-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--c3p0-->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--druid-->
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="Url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username]"></property>
<property name="password" value="${jdbc.root}"></property>
</bean>
<!--jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="druid"></constructor-arg>
</bean>
</beans>
持久层(dao层)------------>AccountDao
public interface AccountDao {
public void delete();
}
daoimpl层----------------->AccountDaoImpl
public class AccountDaoImpl implements AccountDao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void delete() {
//jdbcTemplate = new JdbcTemplate("c3p0");
String sql = "delete from account";
jdbcTemplate.update(sql);
}
}
业务层(service层)------->AccountService
public interface AccountService {
public void delete();
}
serviceimpl层------------>AccountServiceImpl
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void delete() {
accountDao.delete();
}
}
test层---------------------->Test_delete
public class Test01_delete {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountService = (AccountService) ac.getBean("accountService");
accountService.delete();
}
}
test层------------------->Demo
public class Demo {
public static void main(String[] args) throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
DataSource c3p0 = (DataSource) ac.getBean("c3p0");
Connection connection = c3p0.getConnection();
System.out.println(connection);
}
}
test层------------------->Demo_c3p0
public class Demo01_c3p0 {
public static void main(String[] args) throws PropertyVetoException, SQLException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///mybatis");
dataSource.setUser("root");
dataSource.setPassword("root");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
test层---------------------->Demo_druid
public class Demo02_druid {
public static void main(String[] args) throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///mybatis");
dataSource.setUsername("root");
dataSource.setPassword("root");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
}
}
使用半XML半注解(掌握)
bean.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: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">
<!--
半xml半注解:自己的资源用注解代替;别人的资源继续使用xml
条件:spring的注解默认是关闭的 所以得配置一个注解扫描器进行开启和扫描
注解扫描器作用:是用来加载解析已经编写好的注解的 但是得告诉它去哪个包下扫描注解(包含子包)
-->
<!--使用注解的方式来解析自己的资源-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--第三方的资源交给spring的容器管理-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--c3p0-->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--druid-->
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="Url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--JdbcTemplate(连接池)-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--有参构造器传递一个参数(连接池)-->
<constructor-arg name="dataSource" ref="druid"></constructor-arg>
</bean>
</beans>
serviceimpl层----------------->AccountServiceImpl
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {
/* @Autowired 1.会根据自己的类型(接口)去spring容器中找对应的对象自动赋值,
如果@Autowired在成员属性上,那么set方法可以省略不写。
可以设置在成员属性上,也可以设置在set方法上,如果设置在了属性上,set方法可以省略。
2.默认按照类型(接口)从容器中查找对象注入,如果该容器中有多个接口的对象,可以使用一下注解配套。
因为@Autowired注解中不能加 value,所以需要 @Qualifier来指定从哪一个接口注入;
如果只有一个接口,则只需要一个@Autowired即可
@Qualifier 按照指定的唯一标识从容器中查找对象并注入
value :指定唯一标识(id)
*/
/*
JDK提供的注解:@Resource(name)
*/
@Autowired
@Qualifier(value = "accountDao1")
private AccountDao accountDao;
public void delete() {
// 调用dao
accountDao.delete();
}
}
接口一:
@Repository(value = "accountDao1")
public class AccountDaoImpl implements AccountDao{
@Autowired
private JdbcTemplate jdbcTemplate;
public void delete() {
String sql = "delete from account";
jdbcTemplate.update(sql);
}
}
接口二:
@Repository(value = "accountDao2")
public class AccountDaoImpl2 implements AccountDao {
public void delete() {
System.out.println(11111111);
}
}
使用全注解(理解即可)
public class Test02_annotation {
public static void main(String[] args) {
//使用纯注解的方式代替配置文件
//纯注解就是自己的资源和第三方的资源全部用注解
//自己的资源:accountService accountDao
//第三方的资源:c3p0,druid,jdbctemplate
//没有配置文件了,我们应该怎么去加载?使用配置类替换配置文件,加载的时候就加载配置类
//如何加载配置类:Application接口还有一个实现类:AnnotationConfigApplicationContext
//AnnotationConfigApplicationContext:就是用来加载配置类
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService accountService = (AccountService) applicationContext.getBean("accountService");
accountService.delete();
}
}
//@Configuration:配置类(替换配置文件"bean.xml"),会自动执行该类的所有方法
//@ComponentScan(basePackages = "com.itheima"):自动扫描“com.itheima”包下的所有文件
@Configuration
@ComponentScan(basePackages = "com.itheima")//<context:component-scan base-package="cn.itcast"></context:component-scan>
@PropertySource(value = "classpath:jdbc.properties")//<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
@Import(value = JdbcConfig.class)//引入别的配置类
public class SpringConfig {
}
public class JdbcConfig {
//类中的方法都执行
@Value(value = "${jdbc.driver}")//前提:spring要加载外部的properties文件
private String driver;
@Value(value = "${jdbc.url}")//前提:spring要加载外部的properties文件
private String url;
@Value(value = "${jdbc.username}")//前提:spring要加载外部的properties文件
private String username;
@Value(value = "${jdbc.password}")//前提:spring要加载外部的properties文件
private String password;
//创建连接池---给ioc容器管理
@Bean(value = "druid")
public DataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
//创建jdbctemplate
@Bean(value = "jdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Qualifier(value = "druid")DataSource dataSource){
//想要的druid是ioc容器中的那个
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return jdbcTemplate;
}
}
spring-junit
步骤:
导入坐标
Junit:
Spring-test:spring整合junit的支持包
@Runwith(SpringJunit4ClassRunner.class) : 声明spring提供的单元测试环境
@ContextConfiguration : 声明spring的配置信息
Locations:xml配置文件
Classes:配置类的字节码
/*
* 问题点:
* 配置文件谁加载? spring-junit可以
*
* */
@RunWith(value = SpringJUnit4ClassRunner.class) //SpringJUnit4ClassRunner加载配置文件
//@ContextConfiguration(locations = "classpath:bean.xml") //配置文件的位置
@ContextConfiguration(classes = SpringConfig.class) //配置类的位置
public class Spring_junit {
@Autowired
private AccountService accountService;
@Test //删除
public void t1(){
accountService.delete();
}
@Test //查询
public void t2(){
List<Account> list = accountService.find();
for (Account account : list) {
System.out.println(account);
}
}
@Test //修改
public void t3(){
//accountService.update();
}
@Test //添加
public void t4(){
//accountService.save();
}
}