spring boot 連線資料庫 sqlserver2012
阿新 • • 發佈:2018-12-09
在使用spring連線資料庫的時候,採用springboot+mybatis的方式連線資料庫
首先在pom.xml檔案中新增mybatis的啟動器和SQL server資料庫的資料庫驅動jar
<dependency>
<groupId>com.microsoft</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
</dependency >
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</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<User> 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<User> 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<User> 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<User> queryAll() {
List<User> user=userMapper.queryAll();
System.out.println(user);
return user;
}
接下來controller層就不需要寫下去了。可以參照之前寫法繼續。具體的檔案結構如下