当前位置:   article > 正文

eclipse spring Boot 集成 Mybatis连接 SQLserver教程_mybatis集成sqlserver

mybatis集成sqlserver

项目搭建

新建spring boot 工程

引入mybatis sqlserver驱动 spring web

也可以通过pom.xml 手动引入相关依赖

驱动包网址如下

Maven Repository: com.microsoft.sqlserver » sqljdbc4 » 4.0 (mvnrepository.com)

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>3.0.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.microsoft.sqlserver</groupId>
  8. <artifactId>mssql-jdbc</artifactId>
  9. <scope>runtime</scope>
  10. </dependency>

配置application properties

  1. server.port=89
  2. # mybatis 配置
  3. mybatis.type-aliases-package=com.itheima.entity
  4. mybatis.mapper-locations=classpath:com/itheima/dao.xml
  5. mybatis.configuration.map-underscore-to-camel-case=true
  6. ## -------------------------------------------------
  7. ## SqlServer 配置
  8. spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433;databasename=xxxx
  9. spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
  10. spring.datasource.username=xxx
  11. spring.datasource.password=xxxx

(xxx的地方自己填写)

建立sql数据表

测试用 :数据库名 ceshi  表名student 列sno varchar类型数字 随便填了几个

 建立sno实体类 

 dao层xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  5. <mapper namespace="com.itheima.dao.Userdao">
  6. <resultMap id="UserMap" type="com.itheima.entity.sno">
  7. <id property="sno" column="sno" javaType="java.lang.String"></id>
  8. </resultMap>
  9. <select id="getallsno" resultMap="UserMap">
  10. select * from student;
  11. </select>
  12. </mapper>

namespace填写dao接口位置 select的id填写接口方法

dao层接口

 接下来写service 层和controllerceng

service接口:

  1. public interface iuserservice {
  2. public List<sno> getallsno();
  3. }

service:

  1. @Primary
  2. @Service
  3. public class userserviceimpl implements iuserservice{
  4. @Autowired
  5. private Userdao usdo;
  6. @Override
  7. public List<sno> getallsno() {
  8. // TODO Auto-generated method stub
  9. return usdo.getallsno();
  10. }
  11. }

controller

  1. import java.util.List;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.itheima.entity.sno;
  8. import com.itheima.service.iuserservice;
  9. @RestController
  10. @ResponseBody
  11. public class usercontroller {
  12. @Autowired
  13. private iuserservice userservice;
  14. @RequestMapping(value = "/getallsno", method = RequestMethod.GET)
  15. public List<sno> getallsno(){
  16. return userservice.getallsno();
  17. };
  18. }

此时通过postman测试接口

成功

还有一种可能会显示控制台报错报错

 这个时候可以打开你的jdk文件夹 找到java.security文件并打开(低版本jdk在lib 高版本在conf/security)

用记事本打开java.security 

 去掉TLSv1 和TLSv1.1即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/66967
推荐阅读
相关标签
  

闽ICP备14008679号