前言

小项目或者做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的更多相关文章

  1. 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合

    在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...

  2. 基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD

    完成一个RESTful服务,提供几个访问接口,用来操作较简单的联系人信息,数据保存在Sql Server数据库中. 1.使用STS创建工程. 使用STS创建RESTful工程,可以参考: <用S ...

  3. Spring连接MySQL、Oracle和SQL Server的数据库运动连接属性

    在配置文件applicationContext.xml设置如下:<?xml version="1.0" encoding="UTF-8"?>< ...

  4. Spring连接MySQL、Oracle和SQL Server

    其中applicationContext.xml的配置如下: <?xml version="1.0" encoding="UTF-8"?> < ...

  5. ADO.NET 快速入门(十二):从 SQL Server 生成 XML 数据

    本文演示如何使用2种不同的方法从 SQL Server 生成 XML.   方法1:使用了 SqlCommand 的 ExecuteXmlReader 方法获取 XmlReader,然后使用 Data ...

  6. Spring MVC(二)--Spring MVC登陆实例

    本文通过一个简单的登陆实例实现Spring MVC的流程,同时整合 MyBatis使用,流程是这样的: 1.访问一个URL进入登陆界面 2.输入正确的用户名和密码,成功则进入index页面,否则留在登 ...

  7. SQL Server 性能优化实战系列(二)

    SQL Server datetime数据类型设计.优化误区 一.场景 在SQL Server 2005中,有一个表TestDatetime,其中Dates这个字段的数据类型是datetime,如果你 ...

  8. SQL Server 创建数据库邮件

    一. 背景 数据库发邮件通知数据库的运行状态(状态可以通过JOB形式获取)和信息,达到预警的效果. 二. 基础知识 msdb系统数据库保存有关Job,Database Mail,Nodifyicati ...

  9. SQL Server基础之《视图的概述和基本操作》

     数据库中的视图是一个虚拟表.同真实的表一样,视图包含一系列带有名称的列和行数据,行和列数据用来自由定义视图和查询所引用的表,并且在引用视图时动态产生.本篇将通过一些实例来介绍视图的概念,视图的作用, ...

随机推荐

  1. BeanUtil拷贝

    拷贝vo对象 一些查询到的数据很多是不需要的,可以创建vo对象,对需要的对象属性进行拷贝 maven依赖 <dependency> <groupId>org.projectlo ...

  2. ABP框架系列之三:(Entity Framework Integration-实体框架集成)

    ASP.NET Boilerplate can work with any O/RM framework. It has built-in integration with EntityFramewo ...

  3. MySQL 查看修改字符集

    查看MYSQL数据库服务器和数据库字符集 方法一:show variables like '%character%'; 方法二:show variables like 'collation%'; sh ...

  4. 前端之html表单

    html表单 用于搜集不同类型的用户输入 表单由不同类型的标签组成   1.<form>标签 定义整体的表单区域     * action属性 定义表单数据提交地址     * metho ...

  5. Itween的代码使用方法 - 01

    BB:Itween是真心不好用! - 透明度动画 void Start () { //键值对儿的形式保存iTween所用到的参数 Hashtable args = new Hashtable(); / ...

  6. String str.trim()

    String.trim() 方法不仅仅是去除字符串两端的空格字符,它能去除25种字符: ('/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', ...

  7. 利用树莓派3和RTL-SDR V3搭建一个低成本的QRP小功率监测点

    TUTORIAL: SETTING UP A LOW COST QRP (FT8, JT9, WSPR ETC) MONITORING STATION WITH AN RTL-SDR V3 AND R ...

  8. Javascript高级编程学习笔记(6)—— 流程控制语句

    话不多说,我们直接开始进入今天的主题 流程控制语句 首先什么是流程控制语句呢? 顾名思义,就是控制流程的语句. 在JS中语句定义了ECMAScript中的主要语法,让我们可以使用一系列的关键字来完成指 ...

  9. Go语言运算符

    目录 算术运算符 注意事项 赋值运算符 逻辑运算符 短路与和短路或 关系运算符 位运算符 其他运算符 运算符优先级 运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有:算术运算符.赋 ...

  10. 周末,说声php的setter&getter(魔术)方法,你们辛苦了

    php 作为快速迭代项目的语言,其牛逼性质自不必多说.今天咱们要来说说php语言几个魔术方法,当然了,本文主要以setter&getter方法说明为主. 首先,咱们得知道什么叫魔术方法? 官方 ...