Spring增删改查
程序员文章站
2022-07-01 12:46:19
...
Spring增删改查
新建一个项目spring 新建的时候要勾选web
在web里新建一个lib包,导入以下jar包
新建以下包和JAVA文件
增删改查实现的代码如下
User中代码
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserDao中代码
@Component
public class UserDao implements IUserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> findAll() {
String sql="select * from tb_user";
BeanPropertyRowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
List<User> users=jdbcTemplate.query(sql,rowMapper);
return users;
}
@Override
public void delete(int id) {
String sql="delete from tb_user where id=?";
jdbcTemplate.update(sql,id);
}
@Override
public void add(String username, String password) {
String sql="insert into tb_user(username,password) values (?,?)";
jdbcTemplate.update(sql,username,password);
}
@Override
public void update(int id, String username, String password) {
String sql="update tb_user set username=?,password=? where id=?";
jdbcTemplate.update(sql,username,password,id);
}
}
IUserDao中代码
public interface IUserDao {
List<User> findAll();
void delete(int id);
void add(String username,String password);
void update(int id,String username,String password);
}
UserService中代码
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAll() {
List<User> users=userDao.findAll();
return users;
}
@Override
public void delete(int id) {
userDao.delete(id);
}
@Override
public void add(String username, String password) {
userDao.add(username,password);
}
@Override
public void update(int id, String username, String password) {
userDao.update(id,username,password);
}
}
IUserService中
public interface IUserService {
List<User> findAll();
void delete(int id);
void add(String username,String password);
void update(int id,String username,String password);
}
Test 中代码
public class Test {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
IUserService userService=(IUserService) context.getBean("userService");
// userService.findAll();
// User user= (User) context.getBean("user");
// System.out.println(user);
// IUserDao userDao=(IUserDao) context.getBean("userDao");
// List<User> users=userDao.findAll();
// userDao.delete(4);
// System.out.println(users);
// IUserService userService= (IUserService) context.getBean("userService");
// List<User> users1=userService.findAll();
// System.out.println(users1);
// userService.update(6,"aa","123");
// List<User> users2=userService.findAll();
// System.out.println(users2);
// IUserService userService= (IUserService) context.getBean("userService");
List<User> users=userService.findAll();
System.out.println(users);
userService.add("ccc","ccc");
List<User> users1=userService.findAll();
System.out.println(users1);
}
}
spring.xml中代码
<bean id="userService" class="com.zhongruna.service.Impl.UserService"></bean>
<bean id="user" class="com.zhongruna.bean.User">
<property name="id" value="1"></property>
<property name="name" value="abc"></property>
<property name="password" value="123"></property>
</bean>
<context:component-scan base-package="com.zhongruna"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property value="jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8" name="url"/>
<property value="root" name="username"/>
<property value="123456" name="password"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
上一篇: 明朝言官连皇帝都敢大喷特喷 他们为什么从来不敢弹劾藩王
下一篇: 传汇丰银行正在为华为求情
推荐阅读
-
oracle数据库的连接+增删改查操作教程
-
php数据访问之增删改查操作,php数据访问增删_PHP教程
-
PHP MySql增删改查的简单实例_php实例
-
angular 增删改查和验证
-
【精华】Javascript对DOM增删改查,class及CSS操作,一篇全,绝对实用!
-
EasyUI DataGrid结合ThinkPHP实现增删改查操作初学者_html/css_WEB-ITnose
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
-
Java连接MySQL数据库增删改查的通用方法(推荐)
-
JPA之使用JPQL语句进行增删改查