JdbcTemplate简介
JdbcTemplate
是Spring JDBC
的中心类,凭借该类供给的办法能够很便当的完结数据的增修正查。
Spring
对数据库的操作在jdbc
上面做了深层次的封装,运用spring的注入功用,能够把DataSource
注册到JdbcTemplate
之springcloud中。
JspringdbcTemplate
坐落中。其全约束命名为org.springframework.jdbc.core.Jdbcjavaapi中文在线看Templaapplicationte
。要运用JdbcTemlate
还需一个这个包包括了业务和反常操控
Jdbjava言语cTemjava作业培训班plate首要供给以下五类办法:
-
execute办法:能够用于实施任何SQL句子springboot面试题,一般用于实mysql索引行DDL语mysql增删改查句子句;
-
update办法及batchUpdate办法:
update
办法用于实施新增、修改、删去等句子;batchUpdate
办法用于实施批处理相关句子; -
query办法及queryForXXX办法:用于实施查询相关句子;
-
call办法:用于实施存储进程、函数相关句子。
xml中的装备:
<!-- 扫描 -->
<contemysql数据库xt:component-scan base-package="cospring是什么意思m.zzj.*"></context:component-scan>
<!-- 不属于自己工程的政策用bean来配MySQL备 -->
<!-- 装备数据库连java怎样读接池 -->
<bean id="dataSoappointmenturce" class="com.zaxjava作业培训班xer.hikjavascriptari.HikariDataSource" destroy-method="close" p:username="root" p:passspringcloud五大组件word="qw13579wq">
<prmysql索引operty name="driverClassName" value="com.mysql.jdbc.Driver"></property>
&mysql索引lt;propert测验手机是否被监控y name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
</bean>
<!-- 配spring是什么意思备jdbcTemplate -->
<bean cljava模拟器ass="org.springframework.jjava开发dbc.core.JdbcTemplate" p:dataSource-ref="dataSource"></bean>
常用办法:
咱们先创立一个实体政策,对应数据库表中的信息,便当之后的查询操作
package com.zzj.vo;
public class UserInfo {
private int id;
private String userspringbootName;
private String password;
public int getId() {
return id;
}
public vappetiteoid setId(int id) {
thAPPis.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String usermysql暗码忘记了怎样办Name) {
this.userName = userName;
}
public String getPassword() {
return password;
}
p测验郁闷症的20道题ub测验郁闷程度的问卷lic void setJavaPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return测验网速 "UserInfo [id=" + id + ", usermysql装置装备教程Name="mysql创立表 + userName + ", password=" + password + "java模拟器]";
}
}
修改(包括增、删、改):update()办法,springboot还有批量刺进办法batchUpdate()
updatmysql数据库e()办法增修正
:
UserInfoDao.java代码:
@appreciateRepository
public class UsapproacherInfoDao {
@Autowired
//从容器中主动扫描获取jdbcTemplatapplee
private JdbcTemplate jdbcTemplate;
//uappstorepdate()完结增加数据
public boolean insert(int id,String userName,String password){
Strappetiteing sqmysql索引l = "ispringmvc的作业原理nsert into user_info values (?,?,?)";
return jdbcTemplate.updmysql怎样读ate(sql,id,uspringboot面试题serName,password)>0;
}
//update()完结修改
public booleanMySQL update(int id,String userName,String password){
String sql = "update user_info set user_name=springboot面试题?,password=? where id=?";
return jdbcTemplate.update(sql,测验郁闷程度的问卷useJavarName,password,idspring是什么意思)>java模拟器;0;
}
//update()测验郁闷症完结删去
public boolean delete(int id){
String sql = "delete from user_info where id=?";
return jdbcTemplate.update(sql,id)>0;
}
}java言语
测验类代码:
public class Test {
publicspringcloud static void main(String[] args) {
Cl测验assPatspring面试题hXmlApplicationContextappointment applicationContext = new ClassPathXmlApplicationContext("APPapplication.xm测验网速l");
UserInfoDao userDao =java怎样读 applicationConapplicationtext.getBean(UserInfoDao.classappointment);
bo测验你的自卑程度olean insert = userDao.insert(1,"Jim", "123");
boolean update = userDao.update(1,"Tom","123456");
boolean delete = userDao.delete(1测验你的自卑程度);
System.out.printljavaapi中文在线看n(java怎样读"刺进:"+insert+",修改:"+updaappointmentte+",删去:"+delete);
}
}
测验作用:
查询:查询单个值、查询一个政策、查询多个政策
查询单个值
//查询单个值
public boapp是什么意思olean测验你的自卑程度 login(String userName,String password){
tr测验郁闷症y {
String sql = "select id from user_info where usejava模拟器r_name=? and passwJavaord=?测验郁闷症";
jdbjavaapi中文在线看cTemplate.queryForObject(sql,String.class,userName,password);
return true;
} catch (DataAccessException e) {
return falmysql暗码忘记了怎样办se;
}
}
查询一个政策:RowMapper办法和ResultSetExtractor办法
//查询单个政策
public UserInfo getById(int id){
String sql = "select id,user_n测验纸怎样看是否怀孕ame,password from user_info where id=?";
//RowMappjava模拟器er办法
class UserInfoRowMapper implemenapp是什么意思ts RowMapper<UserInfo>{
@Override
public UserInfo mapRow(Res测验郁闷程度的问卷ultSet rs, int rowNum) throws SQLException {java作业培训班
UserInfo userInfo = new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfmysql数据库o.setUserName(rs.getappearanceString("user_name"));
userInfo.setPassword(rs.getStappointmentring("passappstoreword"));
return userInfo;
}
}
return jdbcTemplate.queryForObject(sql,new UserInfoRowMapper(),id);
//RowMapper办法的Laappointmentmbda表达mysql创立表式
retu测验郁闷症的20道题rn jdbcTemplate.queryForObject(sql,(ResultSet rs,mysql怎样读iapproachnt rowNum)->{
UserInfo userInfo = new UserInfo();
userInfo.setId(rs.getIappearancent("id"));
userInfo.setUserName(rs.getString("usmysql装置er_name"));
userInfo.setPassword(rs.测验怀孕的试纸图片一深一浅getString("password"));
return userInfo;
},id);
//ResultSetExtractor办法
class UserInfoResultSet implements ReMySQLsultSetExtractor<UserInfo>{
@Override
public UserInfo extractData(ResultSet rs) throws SQLspringmvc的作业原理Exception, DataAccessExcjavascripteption {
UserInfo userInfo = null;
if(rs.测验郁闷症next()){
userInfo = new UserInfo();
userInfo.setId(rs.appreciategetInt("id"));
userInfo.setUserName(rs.getString("user_namemysql增删改查句子"));
userInfo.setPassword(rs.getString("password"));
}
return userInfo;
}
}
retspringcloud五大组件urn jjava开发dbcTemplate.query(sql,new UserInfoResultSet(),id);
//ResultSetExtractor办法的lambda表达式
return jdbcTemplate.query(sql,(ResultSet rs)->{
UserInfo userInfo = null;
if(rs.next()){
userInfo = new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfojavascript.setUserName(rs.getSspringtring("userAPP_name"));
userInfo.setPassword(rs.getString("password"));
}
return userInfo;
},id);
}测验
查询多个政策:RowMapper办法和ResultSetExtractor办法
//查询多个政策
public L测验郁闷症ist<UserInfjava开发o> selectAll(测验手机是否被监控){
String sql = "select id,user_name,测验工程师password from user_infomysql增删改查句子";
//RowMapper办法
class UserInfoRowMapper implements RowMapper<Userjavaapi中文在线看Info>{
@Override
public UserInfo mapRow(RappearesultSet rs, int rowNum) throws SappstoreQLExcespringcloudption {
UserInfo userInfo = new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfo.setUserName(rs.getString("user_name"));
userInfo.setPassword(rs.getStjava模拟器ring("passwappetiteord"));
return userInfo;
}
}
re测验手机是否被监控turn jdbcTemplate.query(sql,new UserInfoRmysql怎样读owMapper());
//RowMapper办法的Lambdaappetite表达式
returnmysql怎样读 jdbcTemplate.query(sjavaapi中文在线看ql,(ResultSet rs,int rowNum)->{
UserInfo userInfo = new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfo.seMySQLtUserName(rs.getString("user_name"));
userInfo.setPassword(rs.getString("password"));
return userInfo;
});
//ResultSetExtractor办法
class UserInfoResultSet implements ResultSetExtractor<List<UserInfo>>{
@Override
public List<UserInfo&gjava开发t; extractData(ResultSet rs) throws SQLException, DataAccessException {
List<UserInfo> lspringboot常用注解ist = new ArrayList<>();
while(rs.next()){
UserInfo userInfo = new UserIn测验你的自卑程度fo();
userInfo.setId(rs.getInt("i测验郁闷症d"));
userInfo.setUserName(rs.getString("user_name"));
userInfo.setPassword(rs.getString("password"));
list.addmysql装置(usespringmvc的作业原理rInfomysql装置装备教程);
}
return list;
}
}
return jdbcTemplmysql怎样读ate.query(sql, new UserInfoResultSet());
//ResultSetExtractor办法的lambda表mysql暗码忘记了怎样办达式
return jdbcTemplateappear.query(sqapproachl,(ResultSet rs)->{
List&测验网速lt;UserInfo> list = new ArrayList<>();
while(rs.next()){
UserInfo userInfo = new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfo.setUjava言语serName(rs.getString("user_name"));
userInfo.setPassword(rs.getString("password"));
list.add(userInfo);
}
return lijava模拟器st;
});
}
【参看案牍】