若是導入了jpa的依賴,就不用導入jdbctemplete的依賴了jpa的依賴:mysql
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
若是沒有導入jpa的包則要導入jdbcTemplete的包:web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
######################################################## ###datasource ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/mine spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.xujie.pojo.User; @Repository public class UserDao { @Autowired private JdbcTemplate jdbcTemplate; public void save(User user) { String sql = "insert into user (uname) values ('"+user.getUname()+"')"; this.jdbcTemplate.update(sql); } }
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; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public void save(User user) { this.userDao.save(user); } }
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; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/save") public void save() { User user = new User(); user.setUname("yuanxiliu"); this.userService.save(user); } }