spring boot(二): spring boot+jdbctemplate+sql server
前言
小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致。 在spring boot中使用jdbc 连接sql server数据只需要引入两个jar:spring-boot-starter-jdbc、spring-boot-starter-data-jpa
项目结构
和上篇spring boot入门保持相同
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);
} @Override
protected 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\"}";
} } }
spring boot(二): spring boot+jdbctemplate+sql server的更多相关文章
- 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合
在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...
- 基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD
完成一个RESTful服务,提供几个访问接口,用来操作较简单的联系人信息,数据保存在Sql Server数据库中. 1.使用STS创建工程. 使用STS创建RESTful工程,可以参考: <用S ...
- Spring连接MySQL、Oracle和SQL Server的数据库运动连接属性
在配置文件applicationContext.xml设置如下:<?xml version="1.0" encoding="UTF-8"?>< ...
- Spring连接MySQL、Oracle和SQL Server
其中applicationContext.xml的配置如下: <?xml version="1.0" encoding="UTF-8"?> < ...
- ADO.NET 快速入门(十二):从 SQL Server 生成 XML 数据
本文演示如何使用2种不同的方法从 SQL Server 生成 XML. 方法1:使用了 SqlCommand 的 ExecuteXmlReader 方法获取 XmlReader,然后使用 Data ...
- Spring MVC(二)--Spring MVC登陆实例
本文通过一个简单的登陆实例实现Spring MVC的流程,同时整合 MyBatis使用,流程是这样的: 1.访问一个URL进入登陆界面 2.输入正确的用户名和密码,成功则进入index页面,否则留在登 ...
- SQL Server 性能优化实战系列(二)
SQL Server datetime数据类型设计.优化误区 一.场景 在SQL Server 2005中,有一个表TestDatetime,其中Dates这个字段的数据类型是datetime,如果你 ...
- SQL Server 创建数据库邮件
一. 背景 数据库发邮件通知数据库的运行状态(状态可以通过JOB形式获取)和信息,达到预警的效果. 二. 基础知识 msdb系统数据库保存有关Job,Database Mail,Nodifyicati ...
- SQL Server基础之《视图的概述和基本操作》
数据库中的视图是一个虚拟表.同真实的表一样,视图包含一系列带有名称的列和行数据,行和列数据用来自由定义视图和查询所引用的表,并且在引用视图时动态产生.本篇将通过一些实例来介绍视图的概念,视图的作用, ...
随机推荐
- Zookeeper系列1 快速入门
Zookeeper的简介这里我就不说了,在接下来的几篇文章会涉及zookeeper环境搭建,watcher以及相关配置说明, 三种操作zookeeper的方式(原生API方式,zkclient,Cur ...
- JavaScript 方法
对象的定义 var xiaoming = { name: '小明', birth: 1990 }; 是,如果我们给xiaoming绑定一个函数,就可以做更多的事情.比如,写个age()方法,返回xia ...
- Cmd控制台修改编码方法
Cmd控制台修改编码方法 一.前言 在Unbuntu中用sqlite3-command-line操作sqlite3还好好的,到了windows下查询表内容时发现中文全部乱码了!马上想到sqlite3内 ...
- ABP框架系列之四十九:(Startup-Configuration-启动配置)
ASP.NET Boilerplate provides an infrastructure and a model to configure it and modules on startup. A ...
- ota升级动画背景色修改
https://wenku.baidu.com/view/0d63ad25192e45361066f549.html https://blog.csdn.net/huangyabin001/artic ...
- A1280. 最长双回文串
学习了回文树,地址:http://blog.csdn.net/u013368721/article/details/42100363: 这个题就是正这反着加一遍就好,一开始我想的是枚举每个位置,然后一 ...
- scala字符串前加s使用$
https://my.oschina.net/u/2000675/blog/1592140 字符串中的变量替换,Scala中基础的字符串插值就是在字符串前加字幕‘s’,然后在字符串中放入变量,每个变量 ...
- 3 week work—Grid Layout
HTML: <div class="wrapper"> //建立一个三列轨道网格. <div class="one">One</d ...
- redis知识点杂记
最近梳理了一下redis的基本知识.本文会从redis的简单使用.redis的数据类型.redis持久化三个方面做简单阐述和总结. 一.Redis基本操作 1.key的规则 不能使用\n空格.其他都可 ...
- MyBatis 源码分析系列文章导读
1.本文速览 本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章.本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(how)等三个角度进行了说 ...