连接mysql数据库
数据库引入相关 jar 包新建包,创建一些类新建xml文件,进行配置测试与结果 连接mysql数据库
spring框架将JDBC进行了封装,使用 JdbcTemplate 方便实现对数据库操作
(spring5要求jdk不能低于1.8)
所需jar包
导入方法
dao层
UserDao
package com.dao;import com.entity.User;import java.util.List;public interface UserDao { void add(User user); void updateUser(User user); void delete(String id); List
UserDaoImpl
package com.dao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import com.entity.User;import java.util.List;@Repositorypublic class UserDaoImpl implements UserDao { //注入 JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; //添加的方法 @Override public void add(User user) { //1 创建 sql 语句 String sql = "insert into user values(?,?,?)"; //2 调用方法实现 Object[] args = {user.getUserId(), user.getUserName(), user.getUserAge()}; int update = jdbcTemplate.update(sql,args); System.out.println(update); }// 修改 @Override public void updateUser(User user) { String sql="update user set userName=?,userAge=? where userId=?"; Object[] args = {user.getUserName(), user.getUserAge(),user.getUserId()}; int update = jdbcTemplate.update(sql,args); System.out.println(update); }//删除 @Override public void delete(String name) { String sql = "delete from user where =?"; int update = jdbcTemplate.update(sql, name); System.out.println(update); } //查询 @Override public User findone() { String sql = "select * from user where userId='1'"; return jdbcTemplate.queryForObject(sql , new BeanPropertyRowMapper
Service层
package com.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.dao.UserDao;import com.entity.User;import java.util.List;@Servicepublic class UserService { @Autowired private UserDao userDao; public void addUser(User user){ userDao.add(user); } public void updateuser(User user){ userDao.updateUser(user); } public void deleteUser(String id){ userDao.delete(id); } public User findoneInfo(){ return userDao.findone(); }}
创建实体类
entity包下创建类
实体类属性必须和数据库中保持一致
package com.entity;public class User { private String userId; private String userName; private String userAge; public void setUserId(String userId) { this.userId = userId; } public void setUserName(String userName) { this.userName = userName; } public void setUserAge(String userAge) { this.userAge = userAge; } public String getUserId() { return userId; } public String getUserName() { return userName; } public String getUserAge() { return userAge; } }
新建xml文件,进行配置
xml文件在src下(这样做为了方便,也可以放在其他地方,调用的时候注意路径问题就行)
代码
<?xml version="1.0" encoding="UTF-8"?>
在test包下进行测试
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.entity.User;import com.service.UserService;import org.testng.annotations.Test;import java.util.List;public class testUserService {// 增 @Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); User textuser= new User(); textuser.setUserId("1"); textuser.setUserName("赛文"); textuser.setUserAge("16"); userService.addUser(textuser); }}
增加数据结果
删除数据
// 删 @Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); String name="aotomen"; userService.deleteUser(name); }
删除结果
修改数据
// 改 @Test public void test3() { ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); User textuser2 = new User(); textuser2.setUserId("1"); textuser2.setUserName("泰罗"); textuser2.setUserAge("16"); userService.updateuser(textuser2); }
修改结果
查询数据
// 查 @Test public void test4(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); User user =userService.findoneInfo(); System.out.println(user.getUserId()+"n" +user.getUserName()+"n"+user.getUserAge()); }
查询结果