zoukankan      html  css  js  c++  java
  • java-jdbc数据库连接

    web.xml:(web.xml)

    <!-- 加载spring容器 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-shiro.xml</param-value>
        </context-param>

    mybatis.xml:(spring-mybatis.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
        <!-- 导入资源文件 -->
        <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
        <context:property-placeholder location="classpath:jdbc.properties" />
         <!-- 配置C3P0数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 数据库的连接参数 -->
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="initialPoolSize" value="${jdbc.initalPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>
        
         <!--配置NamedParameterJdbcTemplate,该对象可以使用具名参数,其没有无参数的构造器,必须为其构造器指定参数  -->
         <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
             <constructor-arg ref="dataSource"></constructor-arg>
         </bean>
    </beans>

    UserDao.java

    @Repository
    public class UserDao {
        public User findUserByLoginName(String loginName) {
            String sql = "select * from user where loginName = :loginName";
            User user = new User();
            user.setLoginName(loginName);
            //user.setPassWord(passWord);
            SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user);
            BeanPropertyRowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
            new JDBCMySql();
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = JDBCMySql.namedParameterJdbcTemplate;
            try {
                user = namedParameterJdbcTemplate.queryForObject(sql,parameterSource,rowMapper);
            } catch (DataAccessException e) {
                return null;
            }
            return user;
        }
    }
    JDBCMySql.java
    public class JDBCMySql {
        private static ApplicationContext ctx = null;
        public static NamedParameterJdbcTemplate namedParameterJdbcTemplate;
        {
            try {
                if (ctx == null) {
                    ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml");
                }
                namedParameterJdbcTemplate = (NamedParameterJdbcTemplate)ctx.getBean(NamedParameterJdbcTemplate.class);
    
                /*ComboPooledDataSource pool= (ComboPooledDataSource) ctx.getBean("dataSource");
                jdbcUser = pool.getUser();
                jdbcPassword = pool.getPassword();
                jdbcUrl = pool.getJdbcUrl();
                driverClass = pool.getDriverClass();
                initialPoolSize = pool.getInitialPoolSize();
                maxPoolSize = pool.getMaxPoolSize();*/
            } catch (Exception e) {
                System.out.println("错误:" +e.getMessage()+ e.getStackTrace());
            }
        }
    }
  • 相关阅读:
    boostrap各种设备大小,以及不同设备的隐藏和显示
    thinkphp5 layout布局的理解和使用,模板继承等等
    thinkphp5的前台是如何实现的,以及后台栏目的作用
    JS工作流进度条显示,以及相关的思路和原理
    Lnmp服务器集群配置研究和实践
    关于require.js写的最通俗易懂的文章
    使用js把html导出word,单位换算,配置样式和分页,页边距,字体及大小,行距,缩进,@page等等
    使用phpword插件,导出word,并解决循环数据的问题
    php使用array_filter函数,并使用闭包,动态的过滤数组
    leetcode——437. 路径总和 III
  • 原文地址:https://www.cnblogs.com/lijianda/p/9146377.html
Copyright © 2011-2022 走看看