Objective
Small projects or demos can be solved using Jdbc+sql server, which is based on the spring boot environment using JDBC to connect to the SQL Server database, consistent with the Spring MVC series. Using JDBC to connect SQL Server data in spring boot requires only the introduction of two Jar:spring-boot-starter-jdbc, SPRING-BOOT-STARTER-DATA-JPA
Project structure
Keep the same as the previous spring boot entry
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 classApplicationextendsspringbootservletinitializer{ Public Static voidMain (string[] args) {System.out.println ("Server is running at 8080 ..."); Springapplication.run (Application.class, args); } @OverrideprotectedSpringapplicationbuilder Configure (Springapplicationbuilder builder) {returnBuilder.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;@ Repositorypublic class Userdao {@Autowired jdbctemplate jdbctemplate; Public list<user> Listalluser () {list<user> List = jdbctemplate.query ("SELECT * from T_userinfo", New U Ser ()); return list; } public int Insertuser (final User user) {int result = Jdbctemplate.update ("INSERT INTO T_userinfo (name,addres s) VALUES (?,?) ", New PreparedStatementSetter () {public void setvalues (PreparedStatement PS) throws Sqlexcepti On {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=?,addres S=? 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 Objec T[]{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; @Servicepublic 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") publi C 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.li Stalluser (); SYSTEM.OUT.PRINTLN ("Size:" +list.size ()); return list; } @RequestMapping (value= "/insert", method = requestmethod.post) public String insertuser (string name,string addres s) {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\ "}"; } }}
Spring Boot (ii): Spring Boot+jdbctemplate+sql Server