使用SpringMVC轻松处理多种数据库 (springmvc处理数据库)
随着互联网的发展和数据量的不断增加,各种数据库也应运而生。其中常见的关系型数据库有MySQL、Oracle、SQL Server等,非关系型数据库有MongoDB、Redis等。在实际的项目开发中,我们往往需要同时连接和操作多个不同类型的数据库。SpringMVC提供了强大的数据访问层支持,能够轻松处理多种数据库,本文将介绍如何使用SpringMVC实现多种数据库的连接和操作。
1. 引入SpringMVC数据访问支持
在项目中引入SpringMVC数据访问支持,需要在pom.xml文件中添加对应依赖。以下是集成MySQL、Oracle、SQL Server和MongoDB的依赖:
“`
org.springframework
spring-jdbc
5.1.8.RELEASE
mysql
mysql-connector-java
8.0.13
com.oracle
ojdbc6
11.2.0.3
com.microsoft.sqlserver
mssql-jdbc
7.0.0.jre8
org.springframework.data
spring-data-mongodb
2.1.7.RELEASE
org.mongodb
mongodb-driver-sync
3.12.2
“`
其中,spring-jdbc是Spring JDBC的核心包,包含了Spring JDBC的所有功能。mysql-connector-java是MySQL的Java驱动。ojdbc6是Oracle的Java驱动。 mssql-jdbc是SQL Server的Java驱动。spring-data-mongodb是Spring封装的操作MongoDB的Java包。mongodb-driver-sync是MongoDB的Java驱动。
2. 配置数据源
在配置文件中添加数据源相关信息。以下是MySQL、Oracle、SQL Server和MongoDB的配置示例:
MySQL:
“`
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
“`
Oracle:
“`
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=system
spring.datasource.password=123456
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
“`
SQL Server:
“`
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=test
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
“`
MongoDB:
“`
spring.data.mongodb.uri=mongodb://localhost:27017/test
“`
3. 编写Dao层代码
创建对应的Dao类,在其中提供操作数据库的方法,实现数据的增删改查等操作。以下是MySQL、Oracle、SQL Server和MongoDB的Dao类示例:
MySQL:
“`
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List findAll() {
return jdbcTemplate.query(“select * from user”, new BeanPropertyRowMapper(User.class));
}
public void add(User user) {
jdbcTemplate.update(“insert into user (name, age) values (?, ?)”, user.getName(), user.getAge());
}
public void delete(long id) {
jdbcTemplate.update(“delete from user where id = ?”, id);
}
public void update(User user) {
jdbcTemplate.update(“update user set name = ?, age = ? where id = ?”, user.getName(), user.getAge(), user.getId());
}
}
“`
Oracle:
“`
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List findAll() {
return jdbcTemplate.query(“select * from \”user\””, new BeanPropertyRowMapper(User.class));
}
public void add(User user) {
jdbcTemplate.update(“insert into \”user\” (name, age) values (?, ?)”, user.getName(), user.getAge());
}
public void delete(long id) {
jdbcTemplate.update(“delete from \”user\” where id = ?”, id);
}
public void update(User user) {
jdbcTemplate.update(“update \”user\” set name = ?, age = ? where id = ?”, user.getName(), user.getAge(), user.getId());
}
}
“`
SQL Server:
“`
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List findAll() {
return jdbcTemplate.query(“select * from [user]”, new BeanPropertyRowMapper(User.class));
}
public void add(User user) {
jdbcTemplate.update(“insert into [user] (name, age) values (?, ?)”, user.getName(), user.getAge());
}
public void delete(long id) {
jdbcTemplate.update(“delete from [user] where id = ?”, id);
}
public void update(User user) {
jdbcTemplate.update(“update [user] set name = ?, age = ? where id = ?”, user.getName(), user.getAge(), user.getId());
}
}
“`
MongoDB:
“`
@Repository
public class UserDao {
@Autowired
private MongoTemplate mongoTemplate;
public List findAll() {
return mongoTemplate.findAll(User.class);
}
public void add(User user) {
mongoTemplate.save(user);
}
public void delete(User user) {
mongoTemplate.remove(user);
}
public void update(User user) {
mongoTemplate.save(user);
}
}
“`
其中,JdbcTemplate是Spring提供的JDBC模板,能够简化JDBC的操作。BeanPropertyRowMapper是Spring提供的一个实现了ResultSetExtractor接口的类,能够将一个ResultSet对象转换成一个Java对象。MongoTemplate是Spring封装的操作MongoDB的类。
4. 编写Service层代码
创建对应的Service类,在其中调用Dao层提供的方法,实现业务逻辑的处理。以下是MySQL、Oracle、SQL Server和MongoDB的Service类示例:
MySQL:
“`
@Service
public class UserService {
@Autowired
UserDao userDao;
public List findAll() {
return userDao.findAll();
}
public void add(User user) {
userDao.add(user);
}
public void delete(long id) {
userDao.delete(id);
}
public void update(User user) {
userDao.update(user);
}
}
“`
Oracle:
“`
@Service
public class UserService {
@Autowired
UserDao userDao;
public List findAll() {
return userDao.findAll();
}
public void add(User user) {
userDao.add(user);
}
public void delete(long id) {
userDao.delete(id);
}
public void update(User user) {
userDao.update(user);
}
}
“`
SQL Server:
“`
@Service
public class UserService {
@Autowired
UserDao userDao;
public List findAll() {
return userDao.findAll();
}
public void add(User user) {
userDao.add(user);
}
public void delete(long id) {
userDao.delete(id);
}
public void update(User user) {
userDao.update(user);
}
}
“`
MongoDB:
“`
@Service
public class UserService {
@Autowired
UserDao userDao;
public List findAll() {
return userDao.findAll();
}
public void add(User user) {
userDao.add(user);
}
public void delete(User user) {
userDao.delete(user);
}
public void update(User user) {
userDao.update(user);
}
}
“`
5. 编写Controller层代码
创建对应的Controller类,在其中调用Service层提供的方法,实现接口的处理。以下是MySQL、Oracle、SQL Server和MongoDB的Controller类示例:
MySQL:
“`
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(“/user”)
public List findAll() {
return userService.findAll();
}
@PostMapping(“/user”)
public void add(@RequestBody User user) {
userService.add(user);
}
@DeleteMapping(“/user/{id}”)
public void delete(@PathVariable(“id”) long id) {
userService.delete(id);
}
@PutMapping(“/user”)
public void update(@RequestBody User user) {
userService.update(user);
}
}
“`
Oracle:
“`
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(“/user”)
public List findAll() {
return userService.findAll();
}
@PostMapping(“/user”)
public void add(@RequestBody User user) {
userService.add(user);
}
@DeleteMapping(“/user/{id}”)
public void delete(@PathVariable(“id”) long id) {
userService.delete(id);
}
@PutMapping(“/user”)
public void update(@RequestBody User user) {
userService.update(user);
}
}
“`
SQL Server:
“`
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(“/user”)
public List findAll() {
return userService.findAll();
}
@PostMapping(“/user”)
public void add(@RequestBody User user) {
userService.add(user);
}
@DeleteMapping(“/user/{id}”)
public void delete(@PathVariable(“id”) long id) {
userService.delete(id);
}
@PutMapping(“/user”)
public void update(@RequestBody User user) {
userService.update(user);
}
}
“`
MongoDB:
“`
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(“/user”)
public List findAll() {
return userService.findAll();
}
@PostMapping(“/user”)
public void add(@RequestBody User user) {
userService.add(user);
}
@DeleteMapping(“/user”)
public void delete(@RequestBody User user) {
userService.delete(user);
}
@PutMapping(“/user”)
public void update(@RequestBody User user) {
userService.update(user);
}
}
“`
6. 测试接口
启动项目,在浏览器或Postman中访问接口,测试数据的增删改查功能。以下是MySQL、Oracle、SQL Server和MongoDB的测试接口:
MySQL:
“`
GET http://localhost:8080/user
POST http://localhost:8080/user Body: {“name”:”tom”,”age”:18}
PUT http://localhost:8080/user Body: {“id”:1,”name”:”tom”,”age”:19}
DELETE http://localhost:8080/user/1
“`
Oracle:
“`
GET http://localhost:8080/user
POST http://localhost:8080/user Body: {“name”:”tom”,”age”:18}
PUT http://localhost:8080/user Body: {“id”:1,”name”:”tom”,”age”:19}
DELETE http://localhost:8080/user/1
“`
SQL Server:
“`
GET http://localhost:8080/user
POST http://localhost:8080/user Body: {“name”:”tom”,”age”:18}
PUT http://localhost:8080/user Body: {“id”:1,”name”:”tom”,”age”:19}
DELETE http://localhost:8080/user/1
“`
MongoDB:
“`
GET http://localhost:8080/user
POST http://localhost:8080/user Body: {“name”:”tom”,”age”:18}
PUT http://localhost:8080/user Body: {“id”:”5e6cef61f6fb8a43b0e6b9a9″,”name”:”tom”,”age”:19}
DELETE http://localhost:8080/user Body: {“id”:”5e6cef61f6fb8a43b0e6b9a9″,”name”:”tom”,”age”:18}
“`