


Using jdbc to connect to sql server database based on spring boot environment
Preface
Small projects or demos can be solved by using jdbc+sql server. This article uses jdbc to connect to the sql server database based on the spring boot environment, which is consistent with the spring mvc series. To use jdbc in spring boot to connect sql server data, you only need to introduce two jars: spring-boot-starter-jdbc, spring-boot-starter-data-jpa
Project structure
Remains the same as the previous spring boot introduction
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--<exclusions>--> <!--<exclusion>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-tomcat</artifactId>--> <!--</exclusion>--> <!--</exclusions>--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.5.4.RELEASE</version> </dependency> </dependencies>
Application.java, Dao, Service, model
1 ,Application.java
@SpringBootApplication(scanBasePackages = "com.autohome")public class Application extends SpringBootServletInitializer{public static void main(String[] args){ System.out.println("server is running at 8080...."); SpringApplication.run(Application.class,args); } @Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(Application.class); } }
2、Dao
package com.autohome.dao; import com.autohome.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.stereotype.Repository; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Types; import java.util.List; @Repository public class UserDao { @Autowired JdbcTemplate jdbcTemplate; public List<User> listAllUser() { List<User> list = jdbcTemplate.query("select * from t_userinfo",new User()); return list; } public int insertUser(final User user) { int result = jdbcTemplate.update("insert into t_userinfo (name,address) VALUES (?,?)", new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1,user.getName()); ps.setString(2,user.getAddress()); } }); return result; } public int updateUser(final User user) { int result = jdbcTemplate.update("UPDATE t_userinfo set name=?,address=? where id=?", new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1,user.getName()); ps.setString(2,user.getAddress()); ps.setInt(3,user.getId()); } }); return result; } public int deleteUser(int id) { int result = jdbcTemplate.update("delete from t_userinfo where id=?",new Object[]{id},new int[]{Types.INTEGER}); return result; } }
3、Service
package com.autohome.service; import com.autohome.dao.UserDao; import com.autohome.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired UserDao userDao; public List<User> listAllUser(){ return userDao.listAllUser(); } public int insertUser(User user){ return userDao.insertUser(user); } public int updateUser(User user){ return userDao.updateUser(user); } public int deleteUser(int id){ return userDao.deleteUser(id); } }
4、Controller
package com.autohome.controller; import com.autohome.model.User; import com.autohome.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * Created by zhangfei on 2017/6/22. */ @Controller @RequestMapping("/user") public class UserController { @ResponseBody @RequestMapping("/detail") public User detail(Integer id){ User user=new User(); user.setId(id); user.setName("zhangsan"); user.setAddress("china"); return user; } @Autowired UserService userService; @ResponseBody @RequestMapping("/list") public List<User> list(){ List<User> list = userService.listAllUser(); System.out.println("size:"+list.size()); return list; } @RequestMapping(value="/insert",method = RequestMethod.POST) public String insertUser(String name,String address){ User user =new User(); user.setName(name); user.setAddress(address); int result = userService.insertUser(user); if(result>0){ return "{\"returncode\":0,\"message\":\"success\"}"; }else{ return "{\"returncode\":0,\"message\":\"error\"}"; } } }
The above is the detailed content of Using jdbc to connect to sql server database based on spring boot environment. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In 2023, AI technology has become a hot topic and has a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception. With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "SpringAI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "SpringAI", developers can more easily build applications with AI functions, making them easier to use and operate.

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

How to implement spring programmatic transactions: 1. Use TransactionTemplate; 2. Use TransactionCallback and TransactionCallbackWithoutResult; 3. Use Transactional annotations; 4. Use TransactionTemplate in combination with @Transactional; 5. Customize the transaction manager.

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

SpringBoot and SpringCloud are both extensions of Spring Framework that help developers build and deploy microservice applications faster, but they each have different purposes and functions. SpringBoot is a framework for quickly building Java applications, allowing developers to create and deploy Spring-based applications faster. It provides a simple, easy-to-understand way to build stand-alone, executable Spring applications

With the update and iteration of technology, Java5.0 began to support annotations. As the leading framework in Java, spring has slowly begun to abandon xml configuration since it was updated to version 2.5, and more annotations are used to control the spring framework.

How to set the transaction isolation level in Spring: 1. Use the @Transactional annotation; 2. Set it in the Spring configuration file; 3. Use PlatformTransactionManager; 4. Set it in the Java configuration class. Detailed introduction: 1. Use the @Transactional annotation, add the @Transactional annotation to the class or method that requires transaction management, and set the isolation level in the attribute; 2. In the Spring configuration file, etc.

As a Java developer, learning and using the Spring framework is an essential skill. With the popularity of cloud computing and microservices, learning and using Spring Cloud has become another skill that must be mastered. SpringCloud is a development toolset based on SpringBoot for quickly building distributed systems. It provides developers with a series of components, including service registration and discovery, configuration center, load balancing and circuit breakers, etc., allowing developers to build micro
