当前位置:   article > 正文

SpringJdbc查询,_spring jdbc 带套件查询

spring jdbc 带套件查询
SpringJdbc文档


新建一个项目,构架一个Spring,勾选前三个架包。
在项目上新建一个file。起名为:jdbc.properties.


在这个里面直接写:这里以Oracle数据库weili
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
user=system     //这里如果起名usename会报错,貌似这是个关键字。名字可以随便起在配置文件里可以映射
pwd=password




进入配置applicationContext.xml文件里
配置Context的命名空间


<!--加载指定属性 文件(jdbc.properties) -->
<!--定义DataSource组件 -->




配置完毕后。新建一个类User
新建UserRowMapperl类,这个主要用来查询一个表所有的数据。要实现RowMapper接口


新建一个UserDao要继承IdbcDaoSupport,这样更方便,可以直接得到getJdbcTemple()这个数据模板。直接调用的方法




继续在配置文件applicationContext配置JdbcTemplat模板 组件




<!--定义Dao组件,这个UserDao继承了JdbcDaoSupport里面会有 jdbcTemplate这个属性 -->

jdbc.properties

  1. driver=oracle.jdbc.driver.OracleDriver
  2. url=jdbc:oracle:thin:@localhost:1521:orcl
  3. user=system
  4. pwd=password

User类

  1. package entity;
  2. import java.util.Date;
  3. public class User {
  4. private Integer id;
  5. private String username;
  6. private String usepwd;
  7. private String nickname;
  8. private Integer age;
  9. private Date date;
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getUsername() {
  17. return username;
  18. }
  19. public void setUsername(String username) {
  20. this.username = username;
  21. }
  22. public String getUsepwd() {
  23. return usepwd;
  24. }
  25. public void setUsepwd(String usepwd) {
  26. this.usepwd = usepwd;
  27. }
  28. public String getNickname() {
  29. return nickname;
  30. }
  31. public void setNickname(String nickname) {
  32. this.nickname = nickname;
  33. }
  34. public Integer getAge() {
  35. return age;
  36. }
  37. public void setAge(Integer age) {
  38. this.age = age;
  39. }
  40. public Date getDate() {
  41. return date;
  42. }
  43. public void setDate(Date date) {
  44. this.date = date;
  45. }
  46. public User(Integer id, String username, String usepwd, String nickname,
  47. Integer age, Date date) {
  48. super();
  49. this.id = id;
  50. this.username = username;
  51. this.usepwd = usepwd;
  52. this.nickname = nickname;
  53. this.age = age;
  54. this.date = date;
  55. }
  56. public User() {
  57. super();
  58. // TODO Auto-generated constructor stub
  59. }
  60. }

UserRowMapper

  1. package entity;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import org.springframework.jdbc.core.RowMapper;
  5. public class UserRowMapper implements RowMapper<User>{
  6. @Override
  7. public User mapRow(ResultSet rs, int arg1) throws SQLException {
  8. return new User(rs.getInt("id"),rs.getString("username"),rs.getString("userpwd"),rs.getString("nickname"),rs.getInt("age"),rs.getDate("birthday"));
  9. }
  10. }

UserDao

  1. package dao;
  2. import java.util.List;
  3. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  4. import entity.User;
  5. import entity.UserRowMapper;
  6. public class UserDao extends JdbcDaoSupport {
  7. /**
  8. * 查询所有用户
  9. */
  10. public List<User> getAllUser() {//这个UserMapper是提前定义好的
  11. return getJdbcTemplate().query("select * from myuser",new UserRowMapper());
  12. }
  13. }
Test

  1. import java.util.List;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import dao.UserDao;
  5. import entity.User;
  6. public class Test {
  7. public static void main(String[] args) {
  8. //获取AC容器
  9. ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  10. UserDao bean = (UserDao) ac.getBean("userDao");
  11. List<User> allUser = bean.getAllUser();
  12. for (User user : allUser) {
  13. System.out.println(user.getId()+"姓名为:"+user.getUsername()+"昵称为:"+user.getNickname()+"密码为:"+user.getUsepwd()+"年龄为:"+user.getAge()+"生日为:"+user.getDate());
  14. }
  15. }
  16. }

applicationContext.xml配置


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  11. <!--加载指定属性 文件(jdbc.properties) -->
  12. <context:property-placeholder location="classpath:jdbc.properties"/>
  13. <!--定义DataSource组件 ,貌似好几个组件-->
  14. <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"></bean> -->
  15. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  16. <property name="driverClassName" value="${driver}"></property>
  17. <property name="url" value="${url}"></property>
  18. <property name="username" value="${user}"></property>
  19. <property name="password" value="${pwd}"></property>
  20. </bean>
  21. <!--定义JdbcTemplate模板组件 --><!-- ctrl+t搜索 JdbcTemplate出现这个类-->
  22. <bean id="jdbcTemple" class="org.springframework.jdbc.core.JdbcTemplate ">
  23. <property name="dataSource" ref="dataSource"></property>
  24. </bean>
  25. <!--定义Dao组件,这个UserDao继承了JdbcDaoSupport里面会有 jdbcTemplate这个属性 -->
  26. <bean id="userDao" class="dao.UserDao">
  27. <property name="jdbcTemplate" ref="jdbcTemple"></property>
  28. </bean>
  29. </beans>



增删改查其他的

  1. package dao;
  2. import java.util.List;
  3. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  4. import entity.User;
  5. import entity.UserRowMapper;
  6. public class UserDao extends JdbcDaoSupport {
  7. /**
  8. * 查询所有用户
  9. */
  10. public List<User> getAllUser() {//这个UserMapper是提前定义好的
  11. return getJdbcTemplate().query("select * from myuser",new UserRowMapper());
  12. }
  13. /*
  14. * 根据用户名和密码查询指定用户信息
  15. */
  16. public User getUserByNameAndPwd(User user){
  17. String sql="select * from myuser where username=? and userpwd=?";
  18. Object[] params={user.getUsername(),user.getUsepwd()};
  19. return getJdbcTemplate().queryForObject(sql, params,new UserRowMapper());
  20. }
  21. /*
  22. * 按用户模糊查询
  23. */
  24. public List<User>getUserLikeName(String name){
  25. String sql="select * from myuser where username like ?";
  26. Object[]params={"%"+name+"%"};
  27. return getJdbcTemplate().query(sql,params,new UserRowMapper());
  28. }
  29. /*
  30. * 查询用户个数
  31. */
  32. public int FindCount(){
  33. String sql="select count(*) from myuser";
  34. return getJdbcTemplate().queryForInt(sql);
  35. }
  36. /*
  37. * 新增用户
  38. */
  39. public int addUser(User user){
  40. String sql="insert into myuser values(seq_mysuser.nextval,?,?,?,?,?)";
  41. Object[] params={user.getUsername(),user.getUsepwd(),user.getNickname(),user.getAge(),user.getDate()};
  42. return getJdbcTemplate().update(sql,params);
  43. }
  44. }


  1. import java.util.Date;
  2. import java.util.List;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import dao.UserDao;
  6. import entity.User;
  7. public class Test {
  8. public static void main(String[] args) {
  9. // 获取AC容器
  10. ApplicationContext ac = new ClassPathXmlApplicationContext(
  11. "applicationContext.xml");
  12. UserDao bean = (UserDao) ac.getBean("userDao");
  13. List<User> allUser = bean.getAllUser();
  14. for (User user : allUser) {
  15. System.out.println(user.getId() + "姓名为:" + user.getUsername()
  16. + "昵称为:" + user.getNickname() + "密码为:" + user.getUsepwd()
  17. + "年龄为:" + user.getAge() + "生日为:" + user.getDate());
  18. }
  19. // 根据用户名和密码查询
  20. String name = "02";
  21. String pwd = "66";
  22. User user = new User();
  23. user.setUsername(name);
  24. user.setUsepwd(pwd);
  25. // 如果没查到该用户,会报异常。所以这里要抛出一下异常
  26. try {
  27. user = bean.getUserByNameAndPwd(user);
  28. if (user != null) {
  29. System.out.println("存在用户名为:" + user.getUsername() + "的用户");
  30. }
  31. } catch (Exception e) {
  32. System.out.println("该用户不存在");
  33. }
  34. // 模糊查询
  35. List<User> users = bean.getUserLikeName("02");
  36. for (User user2 : users) {
  37. System.out.println("用户名昵称是:" + user2.getUsername() + "用户名是:"
  38. + user2.getUsepwd() + "用户昵称是:" + user2.getAge());
  39. }
  40. // 查询用户个数
  41. int count = bean.FindCount();
  42. System.out.println("用户总个数为:" + count);
  43. //新增用户
  44. User user3=new User(null,"孟振","12345","小梦",18,new Date());
  45. if (bean.addUser(user3)>0) {
  46. System.out.println("新增用户成功!");
  47. }
  48. }
  49. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/576776
推荐阅读
相关标签
  

闽ICP备14008679号