栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-11-10

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-11-10

JdbcTemplate连接数据库

1.导入依赖

​ 有些依赖是用不上的,但是也不会影响项目



    4.0.0

    com.example
    JDBCTemplate-02
    1.0-SNAPSHOT
    JDBCTemplate-02
    war

    
        UTF-8
        1.8
        1.8
        5.7.1
    

    
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
            org.junit.jupiter
            junit-jupiter-api
            ${junit.version}
            test
        
        
            org.junit.jupiter
            junit-jupiter-engine
            ${junit.version}
            test
        

        
        
            org.springframework
            spring-webmvc
            5.3.12
        

            
            
                org.springframework
                spring-context
                5.3.12
            

            
            
                org.springframework
                spring-test
                5.3.12
            

            
            
                org.springframework
                spring-jdbc
                5.3.9
            
            
                org.springframework
                spring-tx
                5.2.7.RELEASE
            

            
            
                org.aspectj
                aspectjweaver
                1.9.5
            
            
                org.aspectj
                aspectjrt
                1.9.5
            


            
            
                junit
                junit
                4.12
            

            
            
                commons-dbutils
                commons-dbutils
                1.6
            

            
            
                com.mchange
                c3p0
                0.9.5.2
            

            
            
                mysql
                mysql-connector-java
                8.0.11
            



    

    
        
            
                org.apache.maven.plugins
                maven-war-plugin
                3.3.1
            
        
    

2.建立属性与数据库表列名对应的实体类 People类

public class People {

    private Integer id;
    private String name;
    private Integer age;
    private  String adress;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public People(Integer id, String name, Integer age, String adress) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.adress = adress;
    }

    public People() {
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                ", adress='" + adress + ''' +
                '}';
    }
}

3.建立一个Dao接口 PeopleDao 用来写查询语句

public interface PeopleDao {
    List findAllPeople();
}

4.写接口的实现类 PeopleDaoImpl 写方法的实现

@Repository  //将PeopleDaoImpl注册到Spring容器中
public class PeopleDaoImpl implements PeopleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List findAllPeople() {
        List peopleList = jdbcTemplate.query("select * from t_people", new RowMapper() {

            @Override
            public People mapRow(ResultSet rs, int i) throws SQLException {
                People people = new People();
                people.setId(rs.getInt("id"));
                people.setName(rs.getString("sname"));
                people.setAge(rs.getInt("age"));
                people.setAdress(rs.getString("address"));
                return people;
            }
        });
        return peopleList;
    }

5.数据库的连接信息 jdbc.properties

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/tangshi?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
userName = root
passWord = 123456

6.编写Spring的主配置文件 beans.xml



    
    

        
         
    


    
    
        
        
        
        
        
    

    
    
        
            
                jdbc.properties
            
        
    


    
    
    
    


**7.测试类 **

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        PeopleDao pd = (PeopleDao) context.getBean("peopleDaoImpl");
        List list = pd.findAllPeople();
        for (People people:list) {
            System.out.println(people);
        }
        }
    }

测试截图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号