使用Maven构建spring JdbcTemplate项目

1.新建Maven项目,配置pom.xml,引入需要的jar包



    4.0.0

    edu.njxz.demo
    springTest
    1.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            5.0.6.RELEASE
        
        
            junit
            junit
            4.12
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            org.springframework
            spring-jdbc
            5.1.2.RELEASE
        
        
            commons-logging
            commons-logging
            1.2
        
    


2.创建并编辑配置文件




     
    

    
    
        
        
        
        
        
        
        
        
    

    
    
        
    

3.创建实体类

public class User {
    private int userId;
    private String userName;
    private String userPwd;
    private String userEmail;

    public int getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
}

4.创建数据访问层Dao

public interface UserDao {
    int update(String sql,Object[] param);
    List query(String sql,Object[] param);
}

5.实现UserDao的接口类

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    //使用配置文件中的jdbc模板
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 更新方法
     * @param sql
     * @param param
     * @return
     */
    public int update(String sql, Object[] param) {
        return jdbcTemplate.update(sql,param);
    }

    /**
     * 查询方法
     * @param sql
     * @param param
     * @return
     */
    public List query(String sql, Object[] param) {
        RowMapper rowMapper=new BeanPropertyRowMapper(User.class);

        return jdbcTemplate.query(sql,rowMapper,param);
    }
}

6.创建测试类

public class Test {
    public static void main(String[] args) {
        //把beans.xml的类加载到容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");

        //从容器获取目标对象
        UserDao userDao= (UserDao) applicationContext.getBean("userDao");

        String insertSql="insert into p_user(user_name,user_pwd,user_email) values(?,?,?) ";
        Object param1[]={"神龟","123456","[email protected]"};

        userDao.update(insertSql,param1);

        String selectSql="select * from p_user ";
        List userList=userDao.query(selectSql,null);
        for(User user:userList){
            System.out.println(user.getUserId()+" "+user.getUserName());
        }
    }
}

7.运行结果
使用Maven构建spring JdbcTemplate项目_第1张图片

你可能感兴趣的:(spring)