sqlite+springBoot+mybatis 的简单使用
    		       		warning:
    		            这篇文章距离上次修改已过430天,其中的内容可能已经有所变动。
    		        
        		                
                以下是一个使用Spring Boot和MyBatis操作SQLite数据库的简单示例。
- 首先,在pom.xml中添加依赖:
<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <!-- SQLite Starter -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>YOUR_SQLITE_JDBC_VERSION</version>
    </dependency>
 
    <!-- MyBatis Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>YOUR_MYBATIS_VERSION</version>
    </dependency>
 
    <!-- Test Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>- 配置application.properties或application.yml:
spring.datasource.url=jdbc:sqlite:path_to_your_database.db
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.jpa.database-platform=org.hibernate.dialect.SQLiteDialect- 创建一个Mapper接口:
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users WHERE id = #{id}")
    User getUserById(@Param("id") int id);
 
    @Insert("INSERT INTO users(name, email) VALUES(#{name}, #{email})")
    @Options(useGeneratedKeys=true, keyProperty="id")
    void insertUser(User user);
 
    // 其他方法...
}- 创建一个Service:
@Service
public class UserService {
 
    @Autowired
    private UserMapper userMapper;
 
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }
 
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }
 
    // 其他方法...
}- 创建一个Controller:
@RestController
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") int id) {
        return userService.getUserById(id);
    }
 
    @PostMapping("/user")
评论已关闭