spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
Posted 明金同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段相关的知识,希望对你有一定的参考价值。
实现要求:
在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象,模拟实现用户的增删改操作,并将结果输出到控制台。要求如下:
- set注入方式实现对象的依赖关系。
- 增删改操作需要实现、提交和关闭操作。
事务的开启
实现思路:
在com.mhys.pojo包下,创建User类。 在com.mhys.dao包下,创建UserDao类。
package com.mhys.bean;
public class Users {
private String userId;
private String userName;
private String password;
private int age;
private String address;
private String flag;
public Users() {
super();
}
public Users(String userId, String userName, String password, int age, String address, String flag) {
super();
this.userId = userId;
this.userName = userName;
this.password = password;
this.age = age;
this.address = address;
this.flag = flag;
}
@Override
public String toString() {
return "Users [userId=" + userId + ", userName=" + userName + ", password=" + password + ", age=" + age
+ ", address=" + address + ", flag=" + flag + "]";
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
}
package com.mhys.Dao;
import com.mhys.bean.Users;
public class UsersDao {
public Users getUserById(String userId){
Users users = new Users("130006", "杨明金", "123456", 20, "云南", "1");
return users;
}
public Boolean insertUser(Users users){
System.out.println("插入数据:"+users.getUserId()+","+users.getUserName()
+","+users.getPassword()+","+users.getAge()+","
+users.getAddress()+","+users.getFlag());
return true;
}
public Boolean updataUserByid(Users users){
System.out.println("修改用户编号:"+users.getUserId()+"的密码为:"+users.getPassword());
return true;
}
public Boolean deleteUserByid(Users users){
System.out.println("删除用户编号:"+users.getUserId()+"的信息");
return true;
}
}
在com.mhys.service包下,创建UserService类。
package com.mhys.Dao;
import com.mhys.bean.Users;
public class UsersService {
private UsersDao usersDao;
public Users getUserInfoById(String userId){
Users users = usersDao.getUserById(userId);
return users;
}
public Boolean insertUserInfo(Users users) {
System.out.println("开启事务");
usersDao.insertUser(users);
System.out.println("提交事务");
System.out.println("关闭事务");
return true;
}
public Boolean updataInfo(Users users) {
System.out.println("开启事务");
usersDao.updataUserByid(users);
System.out.println("提交事务");
System.out.println("关闭事务");
return true;
}
public Boolean deleteInfo(Users users) {
System.out.println("开启事务");
usersDao.deleteUserByid(users);
System.out.println("提交事务");
System.out.println("关闭事务");
return true;
}
public UsersDao getUsersDao() {
return usersDao;
}
public void setUsersDao(UsersDao usersDao) {
this.usersDao = usersDao;
}
}
在applicationContext.xml配置文件中,注册userDao类和userService类到容器。
<bean id="users" class="com.mhys.bean.Users"></bean>
<bean id="usersDao" class="com.mhys.Dao.UsersDao"></bean>
<bean id="usersService" class="com.mhys.Dao.UsersService">
<property name="usersDao" ref="usersDao"></property>
</bean>
在com.mhys.demo.test包下,创建测试类。
package com.mhys.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mhys.Dao.UsersService;
import com.mhys.bean.Users;
public class T1_4_3Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UsersService usersService = (UsersService) context.getBean("usersService");
//查询测试
Users users1 = usersService.getUserInfoById("23");
System.out.println(users1);
//创建测试对象
Users users2 = new Users("2487", "杨明金", "123456", 20, "云南省", "1");
//添加测试
Boolean flag1 = usersService.insertUserInfo(users2);
if (flag1) {
System.out.println("插入成功!");
}
//修改测试
users1.setPassword("1111");
Boolean flag2 = usersService.updataInfo(users2);
if (flag2) {
System.out.println("修改成功!");
}
//删除测试
Boolean flag3 = usersService.deleteInfo(users2);
if (flag3) {
System.out.println("删除成功!");
}
}
}
以上是关于spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段的主要内容,如果未能解决你的问题,请参考以下文章