1 springboot 使用 jdbc
- 添加依賴
<!-- JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 數據庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 配置文件
- Service
package com.tzb.service;
public interface IUserService {
public void register(String username,String password);
}
package com.tzb.service.impl;
import com.tzb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void register(String username, String password) {
String sql = "insert t_user (username,password) values(?,?)";
jdbcTemplate.update(sql, username, password);
}
}
- 控制器
package com.tzb.web.controller;
import com.tzb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService service;
@RequestMapping("register")
@ResponseBody // 返回 json
public String register(String username,String password){
service.register(username,password);
return "success";
}
}
- 運行
package com.tzb.web.controller.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.tzb.service", "com.tzb.web"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2 整合 mybatis
使用 maven jar項目
- 依賴
<!--springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 單元測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybaties -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 數據庫配置文件
#數據庫配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- mapper 接口
package com.tzb.springboot.mapper;
import com.tzb.springboot.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
@Insert("INSERT t_user (username,password) values (#{username},#{password})")
public int save(@Param("username") String username,@Param("password") String password);
@Insert("select * from t_user where username = #{username}")
public User findByUsername(@Param("username") String username);
}
- service
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public void register(String username, String password) {
userMapper.save(username, password);
}
}
- 控制器
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("register")
@ResponseBody // 返回的數據轉爲 json
public String register(String username, String password) {
userService.register(username, password);
return "SUCCESS";
}
}
- App.java
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.tzb.springboot")
@MapperScan(basePackages = "com.tzb.springboot.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.1 mapper 使用 xml 配置
- Mapper 類
public interface UserMapper {
public int save(String username, String password);
public User findByUsername(String username);
/* @Insert("INSERT t_user (username,password) values (#{username},#{password})")
public int save(@Param("username") String username,@Param("password") String password);
@Insert("select * from t_user where username = #{username}")
public User findByUsername(@Param("username") String username);*/
}
- UserMapper.xml
<?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="com.tzb.springboot.mapper.UserMapper" >
<insert id="save">
insert into t_user (username,password) VALUES(#{0},#{1})
</insert>
<select id="findByUsername" resultType="com.tzb.springboot.model.User" parameterType="string">
select * from t_user where username = #{username,jdbcType=VARCHAR}
</select>
</mapper>
注意這裏需要在pom中添加下面代碼
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>