spring-jdbc
2019-12-30 本文已影响0人
充满智慧的白痴
实体类
package com.sflq.model;
public class User {
private String username;
private String password;
}
public User(int age, String username, String password, String status, String score) {
this.age = age;
this.username = username;
this.password = password;
this.status = status;
this.score = score;
}
public User(){}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString(){
return username+password+age+status+score;
}
}
字段映射文件
package com.sflq.mapper;
import com.sflq.model.User;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserMapper implements RowMapper {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setPassword(rs.getString("password"));
user.setUsername(rs.getString("username"));
return user;
}
}
Dao执行sql文件
// 此bean需要注入一个dataSource对象
package com.sflq.daoImpl;
import com.sflq.dao.UserDao;
import com.sflq.mapper.UserMapper;
import com.sflq.model.User;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
public class UserDaoImpl implements UserDao {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
@Override
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
@Override
public void create(String username, Integer age,String password,String status,String score) {
String SQL = "insert into user (username, age,password,status,score) values (?,?,?,?,?)";
jdbcTemplateObject.update( SQL, username, age,password,status,score);
System.out.println("Created Record Name = " + username + " Age = " + age);
}
@Override
public User getUser(Integer id) {
String SQL = "select * from User where id = ?";
User user = (User) jdbcTemplateObject.queryForObject(SQL, new Object[]{id}, new UserMapper());
return user;
}
@Override
public List listUsers() {
String SQL = "select * from user";
List user = jdbcTemplateObject.query(SQL, new UserMapper());
return user;
}
@Override
public void delete(Integer id) {
}
@Override
public void update(Integer id, Integer age) {
}
}
配置dao
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
// 向dao对象中注入一个dataSource对象
<bean id="UserDaoImpl" class="com.sflq.daoImpl.UserDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
引用dao对象
package com.sflq;
import com.sflq.daoImpl.UserDaoImpl;
import com.sflq.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Application {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDaoImpl userDaoImpl = (UserDaoImpl)context.getBean("UserDaoImpl");
System.out.println("------Records Creation--------" );
userDaoImpl.create("Zara", 11,"12345","1231","1231");
System.out.println("------Listing Multiple Records--------" );
List<User> students = userDaoImpl.listUsers();
for (User record : students) {
System.out.print("ID : " + record.getUsername() );
System.out.print(", Name : " + record.getPassword() );
System.out.println(", Age : " + record.getAge());
}
System.out.println("----Updating Record with ID = 2 -----" );
userDaoImpl.update(2, 20);
System.out.println("----Listing Record with ID = 2 -----" );
User user = userDaoImpl.getUser(2);
System.out.print("ID : " + user.getUsername() );
System.out.print(", Name : " + user.getPassword() );
System.out.println(", Age : " + user.getAge());
}
}
总结
使用spring的jdbc框架,其实就是利用org.springframework.jdbc.core.JdbcTemplate;
这个类,该类的构造函数需要一个dataSource对象作为参数,而后可以利用实体进行增删改查操作