@Configuration
public class DataSourcesConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource getDataSources() {
return new DruidDataSource();
}
}
5.编写bean层实体类
public class User {
private Integer id;
private String username;
private String password;
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6.编写Dao层curd
@Mapper
public interface UserDao {
@Insert("INSERT INTO `hehe` (`username`, `password`) VALUES (#{username}, #{password})")
public int addUser(User user);
@Delete("DELETE FROM `hehe` where id=#{id}")
public int deleteById(Integer id);
@Update("UPDATE `hehe` SET `username`='#{username}', `password`=#{password} WHERE (`id`=#{id})")
public int updateUser(User user);
@Select("SELECT * from `hehe` where id=#{id}")
public User queryById(@Param("id") Integer id);
}
7.在Application中编写注册Dao层
@MapperScan(value = "cn.vp.dao")@SpringBootApplication
public class JdbccurdApplication {
public static void main(String[] args) {
SpringApplication.run(JdbccurdApplication.class, args);
}
}
8.在test中测试curd方法的运行
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void addUser() throws Exception {
User user=new User();
user.setUsername("吴艳丽");
user.setPassword("2131");
userDao.addUser(user);
}