赞
踩
IDEA配置SpringBoot实现简单的连接sqlserver数据库并在网页中读取数据库中的内容
目录
注意新版的maven不支持之前的sqljdbc4
的包,建议使用mssql-jdbc
- <project xmlns="<http://maven.apache.org/POM/4.0.0>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>" xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0>
- <http://maven.apache.org/maven-v4_0_0.xsd>">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.springmvc</groupId>
- <artifactId>com.springmvc</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>com.springmvc Maven Webapp</name>
- <url><http://maven.apache.org></url>
- <repositories>
- <repository>
- <id>public</id>
- <name>aliyun nexus</name>
- <url><http://maven.aliyun.com/nexus/content/groups/public/></url>
- <releases>
- <enabled>true</enabled>
- </releases>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>public</id>
- <name>aliyun nexus</name>
- <url><http://maven.aliyun.com/nexus/content/groups/public/></url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.0.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <!-- 导入 UTF-8 -->
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.microsoft.sqlserver</groupId>
- <artifactId>mssql-jdbc</artifactId>
- <version>6.4.0.jre8</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.codehaus.janino</groupId>
- <artifactId>commons-compiler</artifactId>
- <version>2.7.8</version>
- </dependency>
- <dependency>
- <groupId>com.microsoft.sqlserver</groupId>
- <artifactId>mssql-jdbc</artifactId>
- </dependency>
- </dependencies>
- <build>
- <finalName>com.springmvc</finalName>
- </build>
- </project>
- package mvc;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class springdemo {
- public static void main(String[] args)
- { SpringApplication.run(springdemo.class, args); }
- }
- package mvc.model;
-
- public class Student {
- private String id;
- private String name;
- private String email;
-
- public Student(String id, String name, String email) {
- this.id = id;
- this.name = name;
- this.email = email;
- }
-
- public Student() {
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- @Override
- public String toString() {
- return " id:" + id + " name:" + name + " email:" + email;
- }
- }
注意此文件是接口类型
- package mvc.Dao;
- import java.util.List;
- import org.apache.ibatis.annotations.Delete;
- import org.apache.ibatis.annotations.Insert;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import org.apache.ibatis.annotations.Select;
- import org.apache.ibatis.annotations.Update;
- import mvc.model.Student;
- @Mapper
- public interface StudentDao {
- @Select("SELECT * FROM student WHERE name like #{name}")
- List<Student> find(@Param("name") String name);
- @Insert("INSERT INTO student ( id,name,email ) VALUES (#{id},#{name},#{email})")
- int insert(Student stu);
- @Delete("DELETE FROM student WHERE name=#{name}")
- int delete(@Param("name") String name);
- @Update("Update student set student.name=#{name},student.email=#{email} WHERE student.id=#{id}")
- int update(Student stu);
- @Select("SELECT * FROM student")
- List<Student> findall();
-
- }
- package mvc.Service;
- import java.util.List;
-
- import mvc.Dao.StudentDao;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import mvc.model.Student;
- @Service
- public class StudentService {
- @Autowired
- private StudentDao dao;
- public List<Student> findstu(String name) { return
- dao.find(name+'%'); }
- public int insertstu(Student stu) { return dao.insert(stu);}
- public int deletestu(String name) { return dao.delete(name); }
- public int updatestu(Student stu) { return dao.update(stu); }
- public List<Student> findall() { return dao.findall(); }
- }
- package mvc.controller;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.servlet.ModelAndView;
- import mvc.Service.StudentService;
- import mvc.model.Student;
- @RestController
- @RequestMapping(value="/demo")
- public class StudentController {
- @Autowired
- private StudentService svc;
- @RequestMapping(value="/select")
- public ModelAndView select(@RequestParam("name") String name) {
- ModelAndView mav=new ModelAndView("view");
- List<Student>stu=new ArrayList();
- stu=svc.findstu(name);
- mav.addObject("stu",stu);
- return mav;
- }
- @RequestMapping(value="/insert")
- public int insertstu ( @RequestParam("id") String
- id,@RequestParam("name") String name,@RequestParam("email") String email) {
- Student stu=new Student(id, name, email);
- return svc.insertstu(stu);
- }
- @RequestMapping(value="/delete")
- public int deletestu(@RequestParam("name") String name) { return
- svc.deletestu(name); }
- @RequestMapping(value="/update")
- public int updatestu ( @RequestParam("id") String
- id,@RequestParam("name") String name,@RequestParam("email") String email) {
- Student stu=new Student(id,name,email);
- return svc.updatestu(stu);
- }
- @RequestMapping(value="/findall")
- public ModelAndView findall(){
- ModelAndView mav=new ModelAndView("view");
- List<Student>stu=new ArrayList();
- stu=svc.findall();
- mav.addObject("stu",stu);
- return mav;
- }
- }
注意要换成自己的配置信息
- spring.datasource.url=jdbc:sqlserver://localhost;database=Student
- spring.datasource.username=sa
- spring.datasource.password=123
- spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
- server.port=8888
- <table border="1">
- <tr th:each="stu:${stu}">
- <td th:text="${stu.id}"></td> <td th:text="${stu.name}"></td><td
- th:text="${stu.email}"></td>
- </tr>
- </table>
- create database Student;
-
- use Student;
-
- create table Student(
- id varchar(255),
- name varchar(255),
- email varchar(255),
- constraint Student_Pri primary key (id)
- );
-
- create database Student;
-
- user Student;
-
- create table Student(
- id varchar(255),
- name varchar(255),
- email varchar(255),
- constraint Student_Pri primary key (id)
- );
-
- insert into Student(id, name, email)
- values ('20302200001', '张三', '20302200001@m.ldu.edu.cn'),
- ('20302200002', '李四', '20302200002@m.ldu.edu.cn'),
- ('20302200003', '王五', '20302200003@m.ldu.edu.cn'),
- ('20302200004', '李明', '20302200004@m.ldu.edu.cn');
输入地址127.0.0.1:8888/demo/findall
数据库太老要用之前的包
点击Drivers
还是不行,修改我们的jdk文件
参考
找到我们jdk的安装文件夹
进入conf
进入security
记事本打卡java.security
cirl+f搜索“jdk.tls.disabledAlgorithms=
”
删除 TLSv1, TLSv1.1, 3DES_EDE_CBC(存在则删除,有的有,有的可能就只有其中一个)
保存再重连
还一样,更改idea中的该文件步骤一样,在idea安装目录下的\App\IntelliJ IDEA 2023.2.1\jbr\conf\security
连接成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。