赞
赏
搭建 Spring 开发环境,实现基于 XML 配置的 Spring IOC 案例。
搭建 Spring 的开发环境,基于 XML 配置的 Spring IOC,完成对用户表的增删改查。
CREATE TABLE USER (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20),
`password` VARCHAR(20)
)
pom文件
<?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>net.haicoder</groupId>
<artifactId>SpringDemo07</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--IOC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!--JDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--连接池/数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.7</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
具体实现
核心配置文件:
<?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="url" value="jdbc:mysql:///haicoder_db?useUnicode=true&characterEncoding=utf-8"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--创建JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--创建Dao实现类对象-->
<bean id="userDao" class="net.haicoder.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--创建Service实现类对象-->
<bean id="userService" class="net.haicoder.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
实体类:
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
持久层接口:
public interface UserDao {
/**
* 查找用户
*/
List<User> findAll();
/**
* 根据id查询用户
*/
User findById(Integer id);
/**
* 保存用户
*/
void save(User user);
/**
* 更新用户
*/
void update(User user);
/**
* 删除用户
*/
void delete(Integer id);
}
持久层实现类:
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
// set方法注入
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 查找用户
*/
@Override
public List<User> findAll() {
List<User> users = jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
return users;
}
/**
* 根据id查询用户
*/
@Override
public User findById(Integer id) {
User user = jdbcTemplate.queryForObject("select * from user where id = ?",new BeanPropertyRowMapper<>(User.class),id);
return user;
}
/**
* 保存用户
*/
@Override
public void save(User user) {
jdbcTemplate.update("insert into user values(null,?,?)",user.getUsername(),user.getPassword());
}
/**
* 更新用户
*/
@Override
public void update(User user) {
jdbcTemplate.update("update user set username=?,password=? where id=?",user.getUsername(),user.getPassword(),user.getId());
}
/**
* 删除用户
*/
@Override
public void delete(Integer id) {
jdbcTemplate.update("delete from user where id=?",id);
}
}
业务层接口:
public interface UserService {
/**
* 查找用户
*/
List<User> findAll();
/**
* 根据id查询用户
*/
User findById(Integer id);
/**
* 保存用户
*/
void save(User user);
/**
* 更新用户
*/
void update(User user);
/**
* 删除用户
*/
void delete(Integer id);
}
业务层实现类:
public class UserServiceImpl implements UserService {
private UserDao userDao;
// set方法注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* 查找用户
*/
@Override
public List<User> findAll() {
return userDao.findAll();
}
/**
* 根据id查询用户
*
* @param id
*/
@Override
public User findById(Integer id) {
return userDao.findById(id);
}
/**
* 保存用户
*
* @param user
*/
@Override
public void save(User user) {
userDao.save(user);
}
/**
* 更新用户
*
* @param user
*/
@Override
public void update(User user) {
userDao.update(user);
}
/**
* 删除用户
*
* @param id
*/
@Override
public void delete(Integer id) {
userDao.delete(id);
}
}
测试类:
public class DemoTest {
UserService userService;
@Before
public void beforeqFun(){
System.out.println("嗨客网(www.haicoder.net)\n");
// IOC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取业务层对象
userService = (UserService) act.getBean("userService");
}
@Test
public void testFindAll(){
// 查询所有的用户
List<User> users = userService.findAll();
users.forEach(System.out::println);
}
@Test
public void testFindById(){
// 根据id查询用户
User user = userService.findById(1);
System.out.println(user);
}
@Test
public void testInsert(){
User user = new User();
user.setUsername("Tom");
user.setPassword("123456");
userService.save(user);
System.out.println("添加用户成功!");
}
@Test
public void testUpdate(){
User user = new User();
user.setId(2);
user.setUsername("Tom");
user.setPassword("123");
userService.update(user);
System.out.println("更新用户成功!");
}
@Test
public void testDelete(){
userService.delete(2);
System.out.println("删除用户成功!");
}
}
运行代码
查询所有的用户
![15_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/15_JavaEE Spring.png)
根据Id查询用户
![16_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/16_JavaEE Spring.png)
添加用户
执行代码成功,如下图:
![17_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/17_JavaEE Spring.png)
添加用户成功,如下图:
![18_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/18_JavaEE Spring.png)
更新用户
执行代码成功,如下图:
![19_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/19_JavaEE Spring.png)
更新用户成功,如下图:
![20_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/20_JavaEE Spring.png)
删除用户
执行代码成功,如下图:
![21_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/21_JavaEE Spring.png)
删除用户成功,如下图:
![22_JavaEE Spring.png](/uploads/pic/javaee/javaee-spring/22_JavaEE Spring.png)
总结
以上案例基于 XML 配置的 Spring IOC,完成对用户表的增删改查。