spring boot 连接数据库 sqlserver2012

在使用spring连接数据库的时候,采用springboot+mybatis的方式连接数据库
首先在pom.xml文件中添加mybatis的启动器和SQL server数据库的数据库驱动jar

<dependency>
            <groupId>com.microsoftgroupId>
            <artifactId>sqljdbc4artifactId>
            <version>3.0version>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.1version>
        dependency>

然后在resources/application.properties文件中配置数据库 的基本信息。

#SQL server
spring.datasource.url=jdbc:sqlserver://192.168.5.28:1438;DatabaseName=dawnCar
spring.datasource.username=sa
spring.datasource.password=123
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

接下来是在dao层写mapper文件

package com.dawn.VehicleNetworkSystem.Mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;

import com.dawn.VehicleNetworkSystem.pojo.User;

public interface UserMapper {
    /*这些不需要加mapping*/
    //查询语句
    @Select("select * from dawnCar.dbo.info_user")
   List queryAll();
}

为了能找到对应的mapper,需要在springboot主程序添加扫描包的注解@MapperScan

package com.dawn.VehicleNetworkSystem;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;

@SpringBootApplication
@MapperScan(basePackages="com.dawn.VehicleNetworkSystem.Mapper")
public class VehicleNetworkSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(VehicleNetworkSystemApplication.class, args);
    }
}

然后就可去测试是否能从数据库中取值

package com.dawn.VehicleNetworkSystem;


import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.dawn.VehicleNetworkSystem.Mapper.UserMapper;
import com.dawn.VehicleNetworkSystem.pojo.User;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class VehicleNetworkSystemApplicationTests {
  @Autowired
  private UserMapper userMapper;
    @Test
    public void querytest() {
        List users=userMapper.queryAll();
        System.out.println(users);

    }

}

可以将信息反馈到网页,具体做法如下

//这是service曾的代码,抽取这一层的作用就是为了解耦
package com.dawn.VehicleNetworkSystem.Service;

import java.util.List;

import com.dawn.VehicleNetworkSystem.pojo.User;

public interface UserService {
 public boolean checkLogin(String userName,String passWord);
 public List queryAll();

}
//这是service实现类的代码
import java.util.List;

import org.springframework.stereotype.Service;

import com.dawn.VehicleNetworkSystem.Mapper.UserMapper;
import com.dawn.VehicleNetworkSystem.Service.UserService;
import com.dawn.VehicleNetworkSystem.pojo.User;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean checkLogin(String userName, String passWord) {
    if(userName.equals("lyj")&&passWord.equals("123")) return true;
        return false;
    }

    @Override
    public List queryAll() {
        List user=userMapper.queryAll();
        System.out.println(user);
        return  user;
    }

接下来controller层就不需要写下去了。可以参照之前写法继续。具体的文件结构如下
spring boot 连接数据库 sqlserver2012_第1张图片

你可能感兴趣的:(springboot,sqlserver)