springboot使用jdbcTemplate连接数据库
springboot使用jdbcTemplate连接数据库
1.pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>spring-data_JpaAndJdbcTemplate</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-mybatis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.Student:
package com.example.springmybatis.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Student { @Id @GeneratedValue private String name; @Column(nullable = true) private int age; @Column(nullable = true) private String address; private String collage; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCollage() { return collage; } public void setCollage(String collage) { this.collage = collage; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + ", collage='" + collage + '\'' + ", sex='" + sex + '\'' + '}'; } }
4.UserService:
package com.example.springmybatis.service_jdbc; import com.example.springmybatis.entity.Student; public interface UserService { void create(String name, Integer age); void deleteByName(String name); Integer getAllUsers(); void deleteAllUsers(); Student select(String name, int age); }
5.UserServiceImpl:
package com.example.springmybatis.service_jdbc; import com.example.springmybatis.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; import java.sql.ResultSet; import java.sql.SQLException; @Service public class UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; @Override public void create(String name, Integer age) { jdbcTemplate.update("insert into student(NAME, AGE) values(?, ?)", name, age); } @Override public void deleteByName(String name) { jdbcTemplate.update("delete from student where NAME = ?", name); } @Override public Integer getAllUsers() { return jdbcTemplate.queryForObject("select count(1) from student", Integer.class); } @Override public void deleteAllUsers() { jdbcTemplate.update("delete from student"); } @Override public Student select(String name, int age) { return jdbcTemplate.queryForObject("select name, age, address, collage, sex from student where NAME = ? and age = ?", new MyRowMapper(), name, age); } public class MyRowMapper implements RowMapper<Student> { @Override public Student mapRow(ResultSet resultSet, int i) throws SQLException { String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String address = resultSet.getString("address"); String collage = resultSet.getString("collage"); String sex = resultSet.getString("sex"); Student student = new Student(); student.setName(name); student.setAddress(address); student.setAge(age); student.setCollage(collage); student.setSex(sex); return student; } } }
6.UserController:
package com.example.springmybatis.Controller; import com.example.springmybatis.entity.Student; import com.example.springmybatis.service_jdbc.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class UserController { @Autowired private UserService userService; /** * JdbcTemplate连接方式 * @param name * @param age * @return * @throws Exception */ @RequestMapping(value = "/jdbc", method = RequestMethod.GET) @ResponseBody public Student GetNameJdbc(@RequestParam(value = "name", defaultValue = "易中天") String name , @RequestParam(value = "age", defaultValue = "55") int age)throws Exception{ Student user = userService.select(name, age); return user; } }
7.spring boot启动项:
package com.example.springmybatis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringDataJpaAPP { public static void main(String[] args) { SpringApplication.run(SpringDataJpaAPP.class, args); } }
8.sql:
CREATE TABLE `student` ( `name` varchar(100) NOT NULL, `age` int(10) NOT NULL, `address` varchar(100) NOT NULL, `collage` varchar(100) NOT NULL, `sex` varchar(10) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8
程序员的眼里,不止有代码和bug,还有诗与远方和妹子!!!