├── .gitignore
├── README.md
├── src
├── main
│ ├── resources
│ │ ├── mail.properties
│ │ ├── student.sql
│ │ ├── mapper.xml
│ │ ├── application.properties
│ │ └── logback-spring.xml
│ └── java
│ │ └── com
│ │ └── wuwei
│ │ ├── dao
│ │ ├── Dao.java
│ │ └── JDBCDao.java
│ │ ├── service
│ │ ├── BaseService.java
│ │ └── ServiceImpl.java
│ │ ├── Application.java
│ │ ├── entity
│ │ ├── Result.java
│ │ └── Student.java
│ │ ├── controller
│ │ └── Controller.java
│ │ └── util
│ │ ├── JDBCUtils.java
│ │ ├── MailSender.java
│ │ └── HttpClient.java
└── test
│ └── java
│ └── com
│ └── wuwei
│ └── test
│ └── DatabaseConnectionTest.java
├── pom.xml
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # springboot-jdbc-sqlserver
2 | Spring Boot MVC Demo with JDBC and SqlServer
3 |
--------------------------------------------------------------------------------
/src/main/resources/mail.properties:
--------------------------------------------------------------------------------
1 | from=XXX@126.com
2 | to=xxx@qq.com
3 | username=XXX
4 | password=XXXXXX
5 | host=smtp.126.com
--------------------------------------------------------------------------------
/src/main/resources/student.sql:
--------------------------------------------------------------------------------
1 | exec sys.sp_readerrorlog 0, 1, 'listening';
2 | create database student;
3 | use student;
4 |
5 | create table student(
6 | id bigint primary key identity(1,1),
7 | name NVARCHAR(30) not null,
8 | course NVARCHAR(30) not null,
9 | addtime datetime not null default current_timestamp);
10 |
11 | insert into student(name,course)values('Jack','Chinese');
12 | insert into student(name,course)values('Tom','Computer');
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/dao/Dao.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.dao;
2 |
3 | import com.wuwei.entity.Student;
4 | import java.util.List;
5 |
6 | /**
7 | * Data Access Layer
8 | *
9 | * @author Wu Wei
10 | * @date 2017-8-5 21:03:48
11 | */
12 | public interface Dao {
13 |
14 | public int addStudent(Student student);
15 |
16 | public List> findAllStudent();
17 |
18 | public int updateStudent(Student student);
19 |
20 | public int delStudentById(long id);
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/service/BaseService.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.service;
2 |
3 | import com.wuwei.entity.Result;
4 | import com.wuwei.entity.Student;
5 |
6 | /**
7 | * BaseService Layer
8 | *
9 | * @author Wu Wei
10 | * @date 2017-8-6 20:31:29
11 | */
12 | public interface BaseService {
13 |
14 | public Result addStudent(Student student);
15 |
16 | public Result findAllStudent();
17 |
18 | public Result updateStudent(Student student);
19 |
20 | public Result delStudentById(String id);
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/Application.java:
--------------------------------------------------------------------------------
1 | package com.wuwei;
2 |
3 | import org.mybatis.spring.annotation.MapperScan;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | /**
8 | * SpringBoot入口程序
9 | *
10 | * @author Wu Wei
11 | * @date 2017-8-5 14:04:37
12 | */
13 | @SpringBootApplication
14 | @MapperScan(basePackages = "com.wuwei.dao")
15 | public class Application {
16 |
17 | public static void main(String[] args) {
18 | SpringApplication.run(Application.class, args);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/entity/Result.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.entity;
2 |
3 | import java.io.Serializable;
4 |
5 | public class Result implements Serializable {
6 |
7 | private static final long serialVersionUID = 7086445730263059369L;
8 |
9 | private int status;
10 | private Object data;
11 |
12 | public int getStatus() {
13 | return status;
14 | }
15 |
16 | public void setStatus(int status) {
17 | this.status = status;
18 | }
19 |
20 | public Object getData() {
21 | return data;
22 | }
23 |
24 | public void setData(Object data) {
25 | this.data = data;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/resources/mapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 | insert into student(name,course)values(#{name},#{course})
8 |
9 |
10 |
13 |
14 |
15 | update student set name=#{name},course=#{course} where id=#{id}
16 |
17 |
18 |
19 | delete from student where id=#{id}
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # MySql Connection
2 | #spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8
3 | #spring.datasource.username=root
4 | #spring.datasource.password=
5 | #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
6 |
7 | # SqlServer Connection
8 | spring.datasource.url=jdbc:sqlserver://localhost:1434;databaseName=student
9 | spring.datasource.username=sa
10 | spring.datasource.password=123456
11 | spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
12 |
13 | # Connection Pool
14 | spring.datasource.max-idle=10
15 | spring.datasource.max-wait=10000
16 | spring.datasource.min-idle=5
17 | spring.datasource.initial-size=5
18 | spring.datasource.validation-query=SELECT 1
19 | spring.datasource.test-on-borrow=false
20 | spring.datasource.test-while-idle=true
21 | spring.datasource.time-between-eviction-runs-millis=18800
22 | spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
23 |
24 | # Embeded Tomcat
25 | server.port=8181
26 | server.contextPath=/test
27 |
28 | # Mybatis ORM
29 | mybatis.mapper-locations: classpath:mapper.xml
30 |
31 | # logging
32 | logging.level.org.springframework.web=INFO
33 | logging.file=D:\\logs\\log.log
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/entity/Student.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.entity;
2 |
3 | import java.io.Serializable;
4 | import java.sql.Timestamp;
5 |
6 | /**
7 | *
8 | * @author 吴维
9 | * @date 2017-8-13 13:59:32
10 | */
11 | public class Student implements Serializable {
12 |
13 | private static final long serialVersionUID = 6973576143316146251L;
14 |
15 | private long id;
16 | private String name;
17 | private String course;
18 | private Timestamp addtime;
19 |
20 | public long getId() {
21 | return id;
22 | }
23 |
24 | public void setId(long id) {
25 | this.id = id;
26 | }
27 |
28 | public String getName() {
29 | return name;
30 | }
31 |
32 | public void setName(String name) {
33 | this.name = name;
34 | }
35 |
36 | public String getCourse() {
37 | return course;
38 | }
39 |
40 | public void setCourse(String course) {
41 | this.course = course;
42 | }
43 |
44 | public Timestamp getAddtime() {
45 | return addtime;
46 | }
47 |
48 | public void setAddtime(Timestamp addtime) {
49 | this.addtime = addtime;
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return "Student{" + "id=" + id + ", name=" + name + ", course=" + course + ", addtime=" + addtime + '}';
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/test/java/com/wuwei/test/DatabaseConnectionTest.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.test;
2 |
3 | import com.wuwei.entity.Student;
4 | import com.wuwei.util.JDBCUtils;
5 | import java.sql.*;
6 | import java.util.List;
7 | import java.util.logging.Level;
8 | import java.util.logging.Logger;
9 |
10 | /**
11 | * 数据库连接测试
12 | *
13 | * @author Wu Wei
14 | * @date 2017-8-9 11:07:46
15 | */
16 | public class DatabaseConnectionTest {
17 |
18 | public static void main(String[] args) {
19 | queryTest();
20 | }
21 |
22 | public static void queryTest() {
23 | try {
24 | Connection conn = JDBCUtils.getConnection();
25 | String sql = "select * from student where id = ?";
26 | PreparedStatement pre = conn.prepareStatement(sql);
27 | pre.setInt(1, 1);
28 | ResultSet rs = pre.executeQuery();
29 | //结果集转换成实体对象
30 | List> list = JDBCUtils.TranverseToList(rs, Student.class);
31 | //循环遍历结果
32 | for (int i = 0; i < list.size(); i++) {
33 | Student student = (Student) list.get(i);
34 | System.out.println(student);
35 | }
36 | } catch (SQLException | InstantiationException | IllegalAccessException ex) {
37 | Logger.getLogger(DatabaseConnectionTest.class.getName()).log(Level.SEVERE, null, ex);
38 | } finally {
39 | JDBCUtils.close();
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/resources/logback-spring.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | logback
4 |
5 |
6 |
7 |
8 | INFO
9 |
10 |
11 | %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
12 |
13 |
14 |
15 |
16 | ${log.path}
17 |
18 |
19 | ${log.path}-%d{yyyy-MM-dd}.%i.txt
20 |
21 | 10MB
22 | 365
23 | 10GB
24 |
25 |
26 | %date %level [%thread] %logger{36} [%file : %line] %msg%n
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/controller/Controller.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.controller;
2 |
3 | import com.wuwei.entity.Result;
4 | import com.wuwei.entity.Student;
5 | import javax.annotation.Resource;
6 | import org.springframework.web.bind.annotation.RequestBody;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 | import com.wuwei.service.BaseService;
10 | import org.springframework.web.bind.annotation.PostMapping;
11 | import org.springframework.web.bind.annotation.RequestParam;
12 |
13 | /**
14 | * 控制器Controller
15 | *
16 | * @author Wu Wei
17 | * @date 2017-8-8 17:53:21
18 | */
19 | @RestController
20 | @RequestMapping("/student")
21 | public class Controller {
22 |
23 | @Resource
24 | private BaseService service;
25 |
26 | /**
27 | * 新增
28 | *
29 | * @param student
30 | * @return
31 | */
32 | @PostMapping("/addStudent")
33 | public Result addStudent(@RequestBody Student student) {
34 | return service.addStudent(student);
35 | }
36 |
37 | /**
38 | * 查询
39 | *
40 | * @return
41 | */
42 | @PostMapping("/findAllStudent")
43 | public Result findAllStudent() {
44 | return service.findAllStudent();
45 | }
46 |
47 | /**
48 | * 更新
49 | *
50 | * @param student
51 | * @return
52 | */
53 | @PostMapping("/updateStudent")
54 | public Result updateStudent(@RequestBody Student student) {
55 | return service.updateStudent(student);
56 | }
57 |
58 | /**
59 | * 删除
60 | *
61 | * @param id
62 | * @return
63 | */
64 | @PostMapping("/delStudentById")
65 | public Result delStudentById(@RequestParam("id") String id) {
66 | return service.delStudentById(id);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/service/ServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.service;
2 |
3 | import com.wuwei.dao.Dao;
4 | import com.wuwei.entity.Result;
5 | import com.wuwei.entity.Student;
6 | import java.util.List;
7 | import java.util.logging.Level;
8 | import java.util.logging.Logger;
9 | import javax.annotation.Resource;
10 | import org.springframework.stereotype.Service;
11 |
12 | /**
13 | *
14 | * @author Wu Wei
15 | * @date 2017-8-8 18:06:30
16 | */
17 | @Service
18 | public class ServiceImpl implements BaseService {
19 |
20 | @Resource(name = "JdbcDao")
21 | private Dao dao;
22 | private static final Logger logger = Logger.getLogger(ServiceImpl.class.getName());
23 |
24 | @Override
25 | public Result addStudent(Student student) {
26 | Result result = new Result();
27 | try {
28 | int res = dao.addStudent(student);
29 | result.setStatus(res);
30 | } catch (Exception e) {
31 | logger.log(Level.SEVERE, null, e);
32 | }
33 | return result;
34 | }
35 |
36 | @Override
37 | public Result findAllStudent() {
38 | Result result = new Result();
39 | try {
40 | List> students = dao.findAllStudent();
41 | result.setStatus(1);
42 | result.setData(students);
43 | } catch (Exception e) {
44 | logger.log(Level.SEVERE, null, e);
45 | }
46 | return result;
47 | }
48 |
49 | @Override
50 | public Result updateStudent(Student student) {
51 | Result result = new Result();
52 | try {
53 | int res = dao.updateStudent(student);
54 | result.setStatus(res);
55 | } catch (Exception e) {
56 | logger.log(Level.SEVERE, null, e);
57 | }
58 | return result;
59 | }
60 |
61 | @Override
62 | public Result delStudentById(String id) {
63 | Result result = new Result();
64 | try {
65 | int res = dao.delStudentById(Long.parseLong(id));
66 | result.setStatus(res);
67 | } catch (Exception e) {
68 | logger.log(Level.SEVERE, null, e);
69 | }
70 | return result;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/dao/JDBCDao.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.dao;
2 |
3 | import com.wuwei.entity.Student;
4 | import com.wuwei.util.JDBCUtils;
5 | import java.sql.Connection;
6 | import java.sql.PreparedStatement;
7 | import java.sql.ResultSet;
8 | import java.util.List;
9 | import java.util.logging.Level;
10 | import java.util.logging.Logger;
11 | import org.springframework.stereotype.Repository;
12 |
13 | /**
14 | *
15 | * @author Wu Wei
16 | * @date 2017-8-9 17:07:18
17 | */
18 | @Repository("JdbcDao")
19 | public class JDBCDao implements Dao {
20 |
21 | private static final Logger logger = Logger.getLogger(JDBCDao.class.getName());
22 | private static final Connection conn = JDBCUtils.getConnection();
23 |
24 | @Override
25 | public int addStudent(Student student) {
26 | int res = 0;
27 | String sql = "INSERT INTO student(name,course)VALUES(?,?)";
28 | try {
29 | PreparedStatement pre = conn.prepareStatement(sql);
30 | pre.setString(1, student.getName());
31 | pre.setString(2, student.getCourse());
32 | res = pre.executeUpdate();
33 | } catch (Exception ex) {
34 | logger.log(Level.SEVERE, null, ex);
35 | }
36 | return res;
37 | }
38 |
39 | @Override
40 | public List> findAllStudent() {
41 | List> students = null;
42 | String sql = "SELECT * FROM student";
43 | try {
44 | PreparedStatement pre = conn.prepareStatement(sql);
45 | ResultSet rs = pre.executeQuery();
46 | students = JDBCUtils.TranverseToList(rs, Student.class);
47 | } catch (Exception ex) {
48 | logger.log(Level.SEVERE, null, ex);
49 | }
50 | return students;
51 | }
52 |
53 | @Override
54 | public int updateStudent(Student student) {
55 | int res = 0;
56 | String sql = "UPDATE student SET name = ?,course = ? WHERE id = ?";
57 | try {
58 | PreparedStatement pre = conn.prepareStatement(sql);
59 | pre.setString(1, student.getName());
60 | pre.setString(2, student.getCourse());
61 | pre.setLong(3, student.getId());
62 | res = pre.executeUpdate();
63 | } catch (Exception ex) {
64 | logger.log(Level.SEVERE, null, ex);
65 | }
66 | return res;
67 | }
68 |
69 | @Override
70 | public int delStudentById(long id) {
71 | int res = 0;
72 | String sql = "DELETE FROM student WHERE id = ?";
73 | try {
74 | PreparedStatement pre = conn.prepareStatement(sql);
75 | pre.setLong(1, id);
76 | res = pre.executeUpdate();
77 | } catch (Exception ex) {
78 | logger.log(Level.SEVERE, null, ex);
79 | }
80 | return res;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | com.wuwei
5 | SpringBoot-MVC-JDBC-SqlServer
6 | 1.0-SNAPSHOT
7 | jar
8 |
9 | org.springframework.boot
10 | spring-boot-starter-parent
11 | 1.5.6.RELEASE
12 |
13 |
14 |
15 | UTF-8
16 | 1.8
17 | 1.8
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-test
27 | test
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-web
32 |
33 |
34 | org.springframework
35 | spring-jdbc
36 | 4.3.10.RELEASE
37 | jar
38 |
39 |
40 | mysql
41 | mysql-connector-java
42 |
43 |
44 | com.microsoft.sqlserver
45 | mssql-jdbc
46 | 6.1.0.jre8
47 |
48 |
49 | org.mybatis.spring.boot
50 | mybatis-spring-boot-starter
51 | 1.3.0
52 |
53 |
54 |
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-maven-plugin
59 |
60 | true
61 |
62 |
63 |
64 |
65 | SpringBoot-MVC-JDBC-SqlServer
66 |
67 |
--------------------------------------------------------------------------------
/src/main/java/com/wuwei/util/JDBCUtils.java:
--------------------------------------------------------------------------------
1 | package com.wuwei.util;
2 |
3 | import java.sql.*;
4 | import java.lang.reflect.Field;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.logging.Level;
8 | import java.util.logging.Logger;
9 |
10 | /**
11 | * JDBC工具类
12 | *
13 | * @author Wu Wei
14 | * @date 2017-8-9 18:33:13
15 | */
16 | public class JDBCUtils {
17 |
18 | private static final Logger logger = Logger.getLogger(JDBCUtils.class.getName());
19 | public static Connection connection = null;
20 | public static PreparedStatement preparedStatement = null;
21 | public static ResultSet resultSet = null;
22 |
23 | private JDBCUtils() {
24 | }
25 |
26 | /**
27 | * 获取JDBC连接
28 | *
29 | * @return
30 | */
31 | public static Connection getConnection() {
32 | String url = "jdbc:sqlserver://localhost:1434;databaseName=student";
33 | String username = "sa";
34 | String password = "123456";
35 | if (connection != null) {
36 | return connection;
37 | }
38 | try {
39 | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
40 | connection = DriverManager.getConnection(url, username, password);
41 | } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) {
42 | logger.log(Level.SEVERE, null, ex);
43 | }
44 | return connection;
45 | }
46 |
47 | /**
48 | * 关闭资源
49 | */
50 | public static void close() {
51 | try {
52 | if (connection != null) {
53 | connection.close();
54 | connection = null;
55 | }
56 | if (preparedStatement != null) {
57 | preparedStatement.close();
58 | preparedStatement = null;
59 | }
60 | if (resultSet != null) {
61 | resultSet.close();
62 | resultSet = null;
63 | }
64 | } catch (SQLException e) {
65 | logger.log(Level.SEVERE, null, e);
66 | }
67 | }
68 |
69 | /**
70 | * 将结果集转换成实体对象集合
71 | *
72 | * @param rs
73 | * @param clazz
74 | * @throws SQLException
75 | * @throws IllegalAccessException
76 | * @throws InstantiationException
77 | * @return
78 | */
79 | public static List> TranverseToList(ResultSet rs, Class> clazz) throws SQLException, InstantiationException, IllegalAccessException {
80 | //结果集中列的名称和类型的信息
81 | ResultSetMetaData rsm = rs.getMetaData();
82 | int colNumber = rsm.getColumnCount();
83 | List