前言

小项目或者做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. 简单了解下java中的堆、栈和方法区。

    堆.栈.方法区 1,首先了解下java中的数据类型. ①java中的八大基本数据类型:boolean, char , byte, short, int, long , float , double. ...

  2. Awesome Python 中文版

    Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文 ...

  3. nginx简单权限配置

    一.指定ip段 location / { allow 172.17.0.1/24; deny all; } 二.指定认证账户 location / { auth_basic "please ...

  4. MFC中List控件动态填充数据(LVN_GETDISPINFO)

    在使用List控件的过程中,有时候List控件中需要添加大量的数据,如果使用InsertItem填充,会一次性将数据全部添加进List控件中,比较耗时.这里记录下如何动态添加List控件数据. 步骤 ...

  5. ABP框架系列之四:(Repositories-仓库)

    "Mediates between the domain and data mapping layers using a collection-like interface for acce ...

  6. 关于ASP.NET MVC 中JsonResult返回的日期值问题

    最近开始用MVC做项目,在使用 JsonResult返回数据的时候,日期被反射成了/Date 1233455这种格式,遍查网上都是在客户端使用JS来处理这个问题的,这样的话,就需要在每一个涉及到日期的 ...

  7. ZKWeb网页框架1.9正式发布

    1.9.0更新的内容有 更新项目工具 更好的支持Linux 添加工具函数 Exception.ToDetailedString (获取例外的详细信息) Exception.ToSummaryStrin ...

  8. 建立一个类似于天眼的Android应用程序:第4部分 - 持久收集联系人,通话记录和短信(SMS)

    建立一个类似于天眼的Android应用程序:第4部分 - 持久收集联系人,通话记录和短信(SMS) 电话黑客android恶意软件编程黑客入侵linux 随着我们继续我们的系列,AMUNET应用程序变 ...

  9. Android NDK学习(三):Hello World

    版权声明:转载请说明出处:http://www.cnblogs.com/renhui/p/6925810.html 首先编写Jni接口的c文件,此文件命名有些特殊,具体的命名方式可以参考文档来做. # ...

  10. ElasticSearch权威指南学习(文档)

    什么是文档 在Elasticsearch中,文档(document)这个术语有着特殊含义.它特指最顶层结构或者根对象(root object)序列化成的JSON数据(以唯一ID标识并存储于Elasti ...