Spring Boot Entry Persistence layer (iii)
Original address: Spring Boot entry Persistence layer (iii)
Blog address: http://www.extlight.com First, preface
The previous Spring Boot Web chapter (ii) introduces the content of Spring boot web development, and the development of the project is inseparable from the data, so this article begins with the knowledge of persistence layer. Ii. Integration of JdbcTemplate 2.1 Adding Dependencies
<!--JDBC--
<dependency>
<groupId>org.springframework.boot</groupId>
< artifactid>spring-boot-starter-jdbc</artifactid>
</dependency>
<!--mysql Driver pack--
<dependency>
<groupId>mysql</groupId>
<artifactid>mysql-connector-java </artifactId>
</dependency>
2.2 Configuring database Connections
Add in Application.properties:
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Spring.datasource.url=jdbc:mysql://localhost : 3306/SPRINGBOOT?USEUNICODE=TRUE&CHARACTERENCODING=UTF8&SERVERTIMEZONE=UTC
Spring.datasource.username=root
Spring.datasource.password=tiger
Where driver-class-name can be specified, because spring boot automatically recognizes the URL. 2.3 Testing 2.3.1 Building Table
Create a database named Springboot in MySQL and create a user table in the library:
CREATE TABLE ' user ' (
' id ' INT (one) ' NOT null auto_increment,
' username ' VARCHAR () ' is not null,
' password ' varch AR (+) NOT NULL,
' birthday ' DATE not null,
PRIMARY KEY (' id ')
)
collate= ' Utf8_general_ci '
Engine=innodb
auto_increment=3
;
2.3.2 Building Entity class
public class User implements serializable{
private static final long serialversionuid = -6249397911566315813l;
Private Integer ID;
Private String username;
private String password;
Private Date birthday;
}
Setter and Getter methods are omitted here. 2.3.3 DAO Interface
The interface and implementation classes are as follows:
Public interface Userdao {public int insert (user user);
public int Deletebyid (Integer ID);
public int update (user user);
Public User getById (Integer ID);
} @Repository public class Userdaoimpl implements Userdao {@Autowired private jdbctemplate jdbctemplate; @Override public int Insert (user user) {String sql = ' INSERT INTO User (Id,username,password,birthday) Val
UEs (?,?,?,?) ";
return this.jdbcTemplate.update (SQL, User.getid (),
User.getusername (), User.getpassword (), User.getbirthday ()
);
} @Override public int Deletebyid (Integer ID) {String sql = ' Delete from user where id =? ';
Return This.jdbcTemplate.update (Sql,id); } @Override public int update (user user) {String sql = "Update user set PASSWOrd =?
WHERE id =? ";
return this.jdbcTemplate.update (SQL, User.getpassword (),
User.getid ());
} @Override Public user getById (Integer ID) {String sql = ' SELECT * from User where id =? '; return this.jdbcTemplate.queryForObject (SQL, new rowmapper<user> () {@Override public User
Maprow (ResultSet rs, int rowNum) throws SQLException {User user = new User ();
User.setid (Rs.getint ("id"));
User.setusername (rs.getstring ("username"));
User.setpassword (rs.getstring ("password"));
User.setbirthday (rs.getdate ("Birthday"));
return user;
}},id); }
}
2.3.4 Test class:
@RunWith (springrunner.class)
@SpringBootTest