SpringBooot连接本地sqlite数据库
原创
©著作权归作者所有:来自51CTO博客作者抱琴司棋侍书入画的原创作品,请联系作者获取转载授权,否则将追究法律责任
- pom.xml添加依赖
<!-- sqlite -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
- .properties添加配置
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:C:/Users/catface/Desktop/user.db
- 编写代码
// pojo
public class User {
private String username;
private String password;
// setter&getter...
}
// Dao
@Repository
@Mapper
public interface LoginDao {
List<User> getUsers();
}
// mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cc.catface.sbt_test.multi_sql_source.login.dao.LoginDao">
<select id="getUsers" resultType="cc.catface.sbt_test.multi_sql_source.pojo.User">
select username,password from user;
</select>
</mapper>
// service
@Service
public class LoginService {
@Autowired
LoginDao loginDao;
public List<User> getUsers() {
return loginDao.getUsers();
}
}
// controller
@Controller
public class LoginController {
@Autowired
LoginService loginService;
@ResponseBody
@RequestMapping("/getUsers")
public String getUsers() {
return loginService.getUsers().toString();
}
}
- 访问链接
http://localhost:8080/getUsers
查看数据结果