Springboot Integrated JdbcTemplate

Source: Internet
Author: User

Springboot integrate JDBCTEMPLATE0, build Springboot environment (including database dependencies), add dependencies

If you import JPA dependencies, you do not have to import jdbctemplete dependencies that depend on JPA:

<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-jdbc</artifactId></dependency>  

If you do not import the JPA package, import the Jdbctemplete package:

<dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
Second, the configuration file
###########################################################datasource########################################################spring.datasource.url = jdbc:mysql://localhost:3306/minespring.datasource.username = rootspring.datasource.password = rootspring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10
Iii. Persistence layer
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import com.xujie.pojo.User;@Repositorypublic class UserDao {        @Autowired    private JdbcTemplate jdbcTemplate;        public void save(User user) {        String sql = "insert into user (uname) values (‘"+user.getUname()+"‘)";        this.jdbcTemplate.update(sql);    }}
Iv. Service
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.xujie.dao.UserDao;import com.xujie.pojo.User;import com.xujie.service.UserService;@Servicepublic class UserServiceImpl implements UserService {        @Autowired    private UserDao userDao;    @Override    public void save(User user) {        this.userDao.save(user);    }}
Five, Controller
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import com.xujie.pojo.User;import com.xujie.service.UserService;@RestControllerpublic class UserController {        @Autowired    private UserService userService;        @GetMapping("/save")    public void save() {        User user = new User();        user.setUname("yuanxiliu");        this.userService.save(user);    }}

Springboot Consolidation JdbcTemplate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.