SpringIOC 的注解开发
发布于: 2021 年 02 月 03 日
入门案例步骤
1, 创建 Bean 类, 使用注解配置需要容器管理的 Bean 类;
2, 设置容器配置文件, 引入 context 命名空间;
3, 使用容器中的 Bean.
代码实现
1, Bean 类
2, 容器配置文件
3, 测试使用
示例: CRUD 案例, 使用 xml 与注解混合开发
0, maven 依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!--dbutils的依赖-->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
<scope>compile</scope>
</dependency>
</dependencies>
复制代码
1, 工具类
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @Author MaLi
* @Date 2021/2/3
*/
public class DataBaseUtils {
private static DataSource dataSource;
static {
try {
//使用工厂创建DataSource
InputStream inputStream = DataBaseUtils.class.getClassLoader().getResourceAsStream("druid.properties");
Properties prop = new Properties();
prop.load(inputStream);
dataSource = DruidDataSourceFactory.createDataSource(prop);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return dataSource;
}
}
复制代码
2, dao 接口及实现类
import com.nengli51.pojo.User;
import java.sql.SQLException;
import java.util.List;
public interface UserDao {
void addUser(User user) throws SQLException;
void deleteUser(Integer id) throws SQLException;
void updateUser(User user) throws SQLException;
User findUser(Integer id) throws SQLException;
List<User> findAll() throws SQLException;
}
复制代码
import com.nengli51.dao.UserDao;
import com.nengli51.pojo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;
/**
* @Author MaLi
* @Date 2021/2/3
*/
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
@Qualifier(value = "queryRunner")
private QueryRunner queryRunner;
@Override
public void addUser(User user) throws SQLException {
String sql = "insert into user values(null,?)";
queryRunner.update(sql, user.getName());
}
@Override
public void deleteUser(Integer id) throws SQLException {
String sql = "delete from user where id=?";
queryRunner.update(sql, id);
}
@Override
public void updateUser(User user) throws SQLException {
String sql = "update user set name = ?";
queryRunner.update(sql, user.getName());
}
@Override
public User findUser(Integer id) throws SQLException {
String sql = "select * from user where id=?";
return queryRunner.query(sql, new BeanHandler<>(User.class), id);
}
@Override
public List<User> findAll() throws SQLException {
String sql = "select * from user";
return queryRunner.query(sql, new BeanListHandler<User>(User.class));
}
}
复制代码
3, service 接口及实现类
import com.nengli51.pojo.User;
import java.sql.SQLException;
import java.util.List;
public interface UserService {
/**
* 添加User
*
* @param user
*/
void addUser(User user) throws SQLException;
/**
* 删除User
*
* @param id
*/
void deleteUser(Integer id) throws SQLException;
/**
* 修改User
*
* @param user
*/
void updateUser(User user) throws SQLException;
/**
* 查找指定ID的User
*
* @param id
* @return
*/
User findUser(Integer id) throws SQLException;
/**
* 查找所有User
*
* @return
*/
List<User> findAll() throws SQLException;
}
复制代码
import com.nengli51.dao.UserDao;
import com.nengli51.pojo.User;
import com.nengli51.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
/**
* @Author MaLi
* @Date 2021/2/3
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userDaoImpl")
private UserDao userDao;
@Override
public void addUser(User user) throws SQLException {
userDao.addUser(user);
}
@Override
public void deleteUser(Integer id) throws SQLException {
userDao.deleteUser(id);
}
@Override
public void updateUser(User user) throws SQLException {
userDao.updateUser(user);
}
@Override
public User findUser(Integer id) throws SQLException {
return userDao.findUser(id);
}
@Override
public List<User> findAll() throws SQLException {
return userDao.findAll();
}
}
复制代码
4, controller 实现类
import com.nengli51.pojo.User;
import com.nengli51.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import java.sql.SQLException;
import java.util.List;
/**
* @Author MaLi
* @Date 2021/2/3
*/
@Controller
public class UserControllerImpl {
@Autowired
@Qualifier(value = "userServiceImpl")
private UserService userService;
public void addUser(User user) {
try {
userService.addUser(user);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void deleteUser(Integer id) {
try {
userService.deleteUser(id);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void updateUser(User user) {
try {
userService.updateUser(user);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public User findUser(Integer id) {
User user = null;
try {
user = userService.findUser(id);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return user;
}
public List<User> findAll() {
List<User> userList = null;
try {
userList = userService.findAll();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return userList;
}
}
复制代码
5, 配置容器与注解
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: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">
<!--设置需要扫描的包 -->
<context:component-scan base-package="com.nengli51"></context:component-scan>
<!--使用静态工厂获取 dataSource-->
<bean id="dataSource" class="com.nengli51.utils.DataBaseUtils" factory-method="getDataSource"></bean>
<!--获取数据库框架QueryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="user" class="com.nengli51.pojo.User">
<property name="name" value="Mark"></property>
</bean>
</beans>
复制代码
druid.properties
# 数据库连接参数
url=jdbc:mysql://localhost:3306/spring
username=root
password=root
driverClassName=com.mysql.jdbc.Driver
# 连接池的参数
initialSize=10
maxActive=10
maxWait=2000
复制代码
6, 测试类
import com.nengli51.controller.UserControllerImpl;
import com.nengli51.pojo.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @Author MaLi
* @Date 2021/2/3
*/
public class TestCRUDAnnotation {
private ApplicationContext context;
@Before
public void init(){
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testFindAllUser(){
UserControllerImpl controller = (UserControllerImpl) context.getBean("userControllerImpl");
List<User> allUser = controller.findAll();
for (User user : allUser) {
System.out.println(user);
}
}
@Test
public void testFindUser(){
UserControllerImpl controller = (UserControllerImpl) context.getBean("userControllerImpl");
User user = controller.findUser(2);
System.out.println(user);
}
@Test
public void testUpdateUser(){
UserControllerImpl controller = (UserControllerImpl) context.getBean("userControllerImpl");
User user = new User();
user.setId(2);
user.setName("小马哥");
controller.updateUser(user);
}
@Test
public void testDeleteUser(){
UserControllerImpl controller = (UserControllerImpl) context.getBean("userControllerImpl");
controller.deleteUser(1);
}
@Test
public void testAddUser(){
UserControllerImpl controller = (UserControllerImpl) context.getBean("userControllerImpl");
User user = new User();
user.setName("MaHuateng");
controller.addUser(user);
User user1 = new User();
user1.setName("MaYun");
controller.addUser(user1);
}
}
复制代码
划线
评论
复制
发布于: 2021 年 02 月 03 日阅读数: 14
版权声明: 本文为 InfoQ 作者【小马哥】的原创文章。
原文链接:【http://xie.infoq.cn/article/09154198f48b129b2674b2f45】。文章转载请联系作者。
小马哥
关注
自强不息,厚德载物 2018.12.22 加入
像一棵竹子那样, 不断的扎根积累, 活出节节高的人生!
评论 (1 条评论)