Spring和spring部分配置(自用)

Spring配置

webxml的配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
<!--    应用名  -->
    <context-param>
        <param-name>myapp.admin</param-name>
        <param-value>admin1</param-value>
    </context-param>
    
    
<!--    如果使用configguation的注解方式配置,需要在配置中加入解析方式AnnotationConfigWebApplicationContext -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    
<!--    配置的configuration配置类文件地址  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>club.banyuan.hrms.config.SpringConfig</param-value>
    </context-param>
    
<!--    如果用xml方式配置的话,xml文件的地址-->
<!--    <context-param>-->
<!--        <param-name>contextConfigLocation</param-name>-->
<!--        <param-value>classpath*:config/applicationContext.xml</param-value>-->
<!--    </context-param>-->
    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
<!--    servlet配置-->
    <servlet>
        <servlet-name>ServletDemo2</servlet-name>
        <servlet-class>club.banyuan.hrms.servlet.ServletDemo2</servlet-class>
        <init-param>
            <param-name>jdbc.username</param-name>
            <param-value>user</param-value>
        </init-param>

        <init-param>
            <param-name>jdbc.password</param-name>
            <param-value>****</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>RequestHeaderServlet</servlet-name>
        <servlet-class>club.banyuan.hrms.servlet.RequestHeaderServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletDemo2</servlet-name>
        <url-pattern>/s2/test</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>RequestHeaderServlet</servlet-name>
        <url-pattern>/headers/get</url-pattern>
    </servlet-mapping>
    
    
    
<!--    filter配置-->
    <filter>
        <filter-name>AuthenticateFilter</filter-name>
        <filter-class>club.banyuan.hrms.servlet.AuthenticateFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>AuthenticateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>club.banyuan.hrms.servlet.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>name</param-name>
            <param-value>CharacterEncodingFilter</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>club.banyuan.hrms.servlet.MyListener</listener-class>
    </listener>

    
<!--    session存活时间配置-->
<!--    <session-config>-->
<!--        <session-timeout>1</session-timeout>-->
<!--    </session-config>-->

<!--    首页配置-->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

jdbc配置

jdbc的配置文件jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/hrms?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=xxxxxxxxxxxxxxxx(数据库密码)



配置说明:
驱动:
8.0版本以前
com.mysql.jdbc.Driver 

8.0版本以后
com.mysql.cj.jdbc.Driver

url说明:
jdbc:mysql://host:port/dbname?参数名:参数值
举例
jdbc:mysql://localhost:3306/temp?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
说明:
useUnicode:是否使用Unicode字符集
characterEncoding:当useUnicode设置为true时,指定字符编码。
convertToNull:数据库保存了错误的日期格式(如各值均为0)时,有3种处理策略:
1.exception(不指定,则默认)---->默认抛出异常,
2.convertToNull------->将日期转换成NULL值
3.round------->替换成最近的日期即XXXX-01-01  比如:zeroDateTimeBehavior=round 0001-01-01 00:00:00.0


工具类ProUtil:(作用:读取配置文件)

package club.banyuan.hrms.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropUtil {
    private static Properties properties = new Properties();

    static {
        InputStream is = PropUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProp(String key) {
        return properties.getProperty(key);
    }
}


工具类DbUtil:(作用:通过ProUtil读取配置文件,注册驱动,连接数据库,通过cellection方法来获取数据库连接)
package club.banyuan.hrms.util;
import java.sql.*;

public class DbUtil {
    private static String driver = PropUtil.getProp("jdbc.driver");
    private static String url = PropUtil.getProp("jdbc.url");
    private static String username = PropUtil.getProp("jdbc.username");
    private static String password = PropUtil.getProp("jdbc.password");

    static {
        try {
            // 注册驱动
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    public static void release(Connection connection, Statement statement, ResultSet rs) {
        // 关闭资源
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}


				//实际连接数据库

				//写好sql语句(用于筛选所需要的结果集的语句)
				String sql = "SELECT * FROM t_admin";
				
				//tryresouse中开启连接资源connection,prepareStatement,和结果集resultset
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet rs = preparedStatement.executeQuery();
        ) {
        		//迭代器方式遍历结果集,取出结果集内的数据
            while (rs.next()) {
                Integer id = rs.getInt("id");
                String username = rs.getString("username");
                String rolename = rs.getString("rolename");

                // 创建admin对象
                Admin admin = new Admin();
                admin.setId(id);
                admin.setUsername(username);
                admin.setRolename(rolename);
                admins.add(admin);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值