How to play with database in SpringBoot

Posted by coffejor on Wed, 20 Nov 2019 23:01:18 +0100

Using JDBC

Step 1: add dependency

In the previous article, we talked a lot about how to reimport after adding dependency, which is not repeated here

<!-- JDBC -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- Database driven -->
    <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>

application.properties configuration

Create a new file named application.properties under the resources file

# Configuration data jdbc
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=root
spring.datasource.password=******

service

Controller

Using Mybatis

In creating the UserMapper interface, operate the database by annotation

public interface UserMapper {

    @Insert("insert user (username,password) values (#{username},#{password})")
    public void save(@Param("username") String username,  @Param("password") String password);

    @Select("select from user where username=#{username,jdbcType=VARCHAR}")
    public User findByUserName(@Param("username") String username);
}

Create Service

Also create the UserService interface and UserServiceImpl implementation class

public interface UserService {
    public void insert(String username, String password);
    public User select(String username);
}
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void insert(String username, String password) {
        System.out.println(username+"-"+password);
        userMapper.save(username,password);
    }

    @Override
    public User select(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }
}

Create Controller

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("/save")
    @ResponseBody
    public String save(String username,String password){
        userService.insert(username,password);
        return "Save success";
    }

    @RequestMapping("find")
    @ResponseBody
    public User findByUserName(String username){
        User user = userService.select(username);
        return user;
    }
}

Start App

Start App class

@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.lxk.web","com.lxk.service"})
@MapperScan(basePackages = {"com.lxk.mapper"})
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

Visit http: / / localhost: 8080 / user / save? Username = Zhangsan & password = 123456. If Save success appears, it is successful. Then check whether the inserted entry exists in the database

When visiting http: / / localhost: 8080 / user / find? Username = zhangnsan,
Appears {"username":"zhangsan","password":"123456"}

To be continued...

Topics: Programming Spring JDBC MySQL Database