JDBC连接
1、配置连接池时,需要为连接数据库的连接设置四要素,可以直接写死,也可以使用properties文件引入
2、在引入前要注意:
首先应该引入命名空间context
配置maven依赖
<dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.1.10version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.38version> dependency>
配置连接池
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="dateSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis"/> <property name="username" value="root"/> <property name="password" value="1234"/> bean> beans>
使用占位符及引入配置文件后
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/> <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> bean> beans>
db.properties内容如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///mybatis
username=root
password=1234
综合案例
1、代码结构
2、代码内容
@Setter@Getter@AllArgsConstructor@NoArgsConstructor@ToString public class User { private String id; private String username; private String password; private String date; private String salary; }
@Controller public class UserController { @Autowired private IUserService userService; public void service(){ String username="rose"; String password="123"; String id =null; String date="2018-10-10"; String salary ="2000"; User user = new User(id,username,password,date,salary); userService.save(user); } }
@Repository public class UserDaoImpl implements IUserDao { @Autowired private DataSource dataSource; public void save(User user) { try { Connection connection = dataSource.getConnection(); String sql = "insert into user values(null,?,?,?,?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1,user.getUsername()); ps.setString(2,user.getPassword()); ps.setString(3,user.getDate()); ps.setString(4,user.getSalary()); ps.execute(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public interface IUserDao { void save(User user); }
@Service public class UserServiceImpl implements IUserService { @Autowired private IUserDao userDao; public void save(User user) { userDao.save(user); } }
public interface IUserService { void save(User user); }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class App { @Autowired private ApplicationContext context; @Test public void test(){ UserController userController = context.getBean(UserController.class); userController.service(); } }
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/> <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> bean> <bean id="userController" class="com.test.test_anli.controller.UserController"> <property name="userService" ref="userService"/> bean> <bean id="userService" class="com.test.test_anli.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> bean> <bean id="userDao" class="com.test.test_anli.dao.impl.UserDaoImpl"> <property name="dataSource" ref="dateSource"/> bean> beans>
这些代码已经使用了注解模式,可以删掉注解,将@autowired改为@setter,使用xml文件
DI注解
1、在Spring中使用注解来完成DI操作,我们称之为注解自动装配,存在两种用法.。
一:使用Spring框架自身提供的注解:@Autowired
二:使用JavaEE规范提供的注解:@Resource 功能完全一样
2、Autowired和Qualifier标签:
1.通过@Autowired标签可以让Spring自动的把对象需要的属性从Spring容器中找出来,并注入(设置)给该属性。
2.第三方程序:Spring3.0之前,需要手动配置@Autowired注解解析程序;
在Spring3.0开始,Spring就会自动的加入针对@Autowired标签的解析程序。
3.@Autowired标签贴在字段或者setter方法上。
4.@Autowired可以同时为一个属性注入多个对象。
public void setXxx(OtherBean1 other1,OtherBean2 other2) {}
5.使用@Autowired标签可以注入Spring内置的重要对象,比如BeanFactory,ApplicationContext。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration public class SpringTest { @Autowired private ApplicationContext ctx; }
6.默认情况下@Autowired标签必须要能找到对应的对象,否则报错。不过,可使用required=false来避免该问题:
@Autowired(required=false)
7.@Autowired找bean的方式:
(1)首先按照依赖对象的类型找,如果没有找到,默认会报错;如果找到一个匹配的对象,直接注入;
(2)如果在Spring上下文中找到多个匹配(2个或者2个以上)的类型,再按照名字去找,如果没有匹配则报错;
(3)可以通过使用@Qualifier("otherBean")标签来规定依赖对象按照bean的id+类型去找;