zoukankan      html  css  js  c++  java
  • RowMapper问题以及c3po连接问题

      sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类.

      我们在数据库查询中,如果返回的类型是用户自定义的类型是需要包装,如果是Java自定义的类型,如:String则不需要.。之后的hibernate和spring整合了就不会用这个Rowmapper,毕竟太不方便了,只有spring单独使用的时候我们才会用到这个

      如果sping与hibernate 相结合了,基本上是用不到,大多数都是在spring单独使用时用到.

      可以通过建立内部类实现RowMapper接口,RowMapper中有一个mapRow方法,所以实现RowMapper接口一定要实现mapRow方法,而对自定义类的包装就在mapRow方法中实现.

      总结:Spring单独进行数据库操作过于复杂,每次都要进行数据库连接,而且下面没有事物操作,出现异常出现中断,但是数据进行了操作。因此没有hibernate的封装类操作方便,安全,所以这里仅仅是一个练习,了解一下即可。没有必要做深入研究,主要的是spring文件的bean.xml文件注入要进行大量练习和研究,后面的复杂情况才能得心应手。

      具体代码如下

    @Test
        public void testQuery()
        {
            /*加载连接池*/
            ComboPooledDataSource dataSource=new ComboPooledDataSource();
            dataSource.setDescription("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql:///admin");
            dataSource.setUser("root");
            dataSource.setPassword("1234");
            
            JdbcTemplate jdbc=new JdbcTemplate(dataSource);
            
            String sql="select *from jdbc where username=?";
            User user=jdbc.queryForObject(sql,new MyRowMapper(),"yang");
            System.out.println("user:"+user);
        }
        class MyRowMapper implements RowMapper<User>{
    
            public User mapRow(ResultSet rs, int num) throws SQLException {
                String username=rs.getString("username");
                String password=rs.getString("password");
                
                User user=new User();
                user.setUsername(username);
                user.setPassword(password); 
                return user;
            }  
        }

     如果是一个list集合,就需要用到query方法,这个返回的是一个集合

    @Test
        public void testQuery2()
        {
            /*加载连接池*/
            ComboPooledDataSource dataSource=new ComboPooledDataSource();
            dataSource.setDescription("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql:///admin");
            dataSource.setUser("root");
            dataSource.setPassword("1234");
            
            JdbcTemplate jdbc=new JdbcTemplate(dataSource);
            
            String sql="select *from jdbc";
            List<User> list=jdbc.query(sql,new MyRowMapper());
            System.out.println("user:"+list);
        }

    关于c3po连接问题:MVC框架进行数据库插入操作(入门操作)

    UserDao代码:

    import org.springframework.jdbc.core.JdbcTemplate;
    
    public class UserDao {
        private JdbcTemplate jdbcTemplate;
    
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
        public void add()
        {
            String sql="insert into jdbc values(?,?)";
            jdbcTemplate.update(sql,"jsdk","4568");
        }
    }

    UserService代码

    public class UserService {
        private UserDao userDao;
    
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
        public UserDao getUserDao() {
            return userDao;
        }
        public void add()
        {
            userDao.add();
        }
    }

    c3p0.xml代码:

    <!-- 配置c3p0连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 注入里面的属性值 -->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///hibernate"></property>
            <property name="user" value="root"></property>
            <property name="password" value="1234"></property>
        </bean>
    
    
        <!-- 创建service和dao对象 -->
        <bean id="userService" class="cn.ismy.c3p0.UserService">
            <property name="userDao" ref="userDao"></property>
        </bean>
    
        <bean id="userDao" class="cn.ismy.c3p0.UserDao">
            <!-- 在dao里面注入jdbcTemplate模板对象 -->
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        
        <!-- 创建jdbcTemplate模板对象 用jdbcTemplate类进行crud(增删改查)操作-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!-- 把DataSource传到jdbcTemplate里面去 -->
            <property name="dataSource" ref="dataSource"></property>
        </bean>

    测试类

    public void queryC3p0() {  
            ApplicationContext context=new ClassPathXmlApplicationContext("c3p0.xml");
            UserService uservice=(UserService) context.getBean("userService");
            uservice.add();
        }
  • 相关阅读:
    第27篇-虚拟机字节码指令之操作数栈管理指令
    第26篇-虚拟机对象操作指令之putstatic
    第25篇-虚拟机对象操作指令之getfield
    第24篇-虚拟机对象操作指令之getstatic
    第23篇-虚拟机字节码指令之类型转换
    第22篇-虚拟机字节码之运算指令
    第20篇-加载与存储指令之ldc与_fast_aldc指令(2)
    第19篇-加载与存储指令(1)
    第18章-x86指令集之常用指令
    第17章-x86-64寄存器
  • 原文地址:https://www.cnblogs.com/yq12138/p/7219731.html
Copyright © 2011-2022 走看看