javaee配置文件

Posted 望北i

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaee配置文件相关的知识,希望对你有一定的参考价值。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- dataSource 配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!-- 基本属性 url、user、password -->
        <property name="driverClassName" value="$jdbc.driver"></property>
        <property name="url" value="$jdbc.url"/>
        <property name="username" value="$username"/>
        <property name="password" value="$password"/>
    </bean>
    <!-- 对dataSource 数据源进行事务管理 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 启用对事务注解的支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 扫描mapper包-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basepackage" value="cn.test.mapper"/>
	</bean>
	
	<!-- 扫描service包 -->
	<context:component-scan base-package="cn.test.service"/>
	
	<!-- 配置mybatis工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入数据源 -->
	<property name="dataSource " ref="dataSource"/>
	<!--配置分页插件PageHelper -->
	<property name="plugins">
	<bean class="com.github.pagehelper.PageHelper">
	<property name="properties">
	<props>
	<prop key="dialect">mysql</prop><!-- 配置数据库类型 -->
	<prop key="reasonable">true</prop><!-- 配置页码合理化修正 -->
	</props>
	</property>
	</bean>
	</property>
	</bean> 
</beans>

springMvc.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: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.0.xsd
                           http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	
	
	<!-- 组件扫描,扫描controller包 -->
    <context:component-scan
    	base-package="cn.exam.controller"/>
    
    <!-- 开启mvc注解驱动 -->
    <mvc:annotation-driven/>

	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

	<!-- 不拦截静态资源 -->
	<mvc:default-servlet-handler/>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user_db
jdbc.username=root
jdbc.password=12345

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>review</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 过滤器 -->
	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置spring监听器 -->
	<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 配置加载类路径的配置文件 -->
	<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
</web-app>

userMapper.java

package cn.test.mapper;

import java.util.List;
import cn.test.po.User;

public interface UserMapper 
	public User queryUserByUid(int uid);

	public User getUserByUid(int uid);

	public List<User> getAllUser();

	public List<User> queryByUname(String uname);

	public int addUser(User user);

	public int deleteUser(int uid);

	public int updateUser(User user);


userMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.test.mapper.UserMapper">
	<select id="queryUserByUid" parameterType="integer"
		resultType="cn.test.po.User">
		select * from user_tab where uid=#_parameter
	</select>

	<select id="queryByUname" parameterType="String"
		resultType="cn.test.po.User">
		select * from user_tab where uname like concat('%','#_parameter','%')
	</select>

	<select id="getAllUser" resultType="cn.test.po.User">
		select * from user_tab
	</select>

	<insert id="addUser" parameterType="cn.test.po.User">
		insert into user_tab(uname,age) values(#uname,#age)
	</insert>

	<delete id="deleteUser" parameterType="integer">
		delete from user_tab where uid=#_parameter
	</delete>

	<update id="updateUser" parameterType="cn.test.po.User">
		update user_tab set uname=#uname,age=#age where uid=#uid
	</update>
</mapper>

userService.java

package cn.test.service;

import java.util.List;

import cn.test.po.User;

public interface UserService 
	public User queryUserByUid(int uid);

	public User getUserByUid(int uid);

	public List<User> getAllUser();

	public List<User> queryByUname(String uname);

	public int addUser(User user);

	public int deleteUser(int uid);

	public int updateUser(User user);

userServiceImpl.java

package cn.test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.test.mapper.UserMapper;
import cn.test.po.User;

@Service
public class UserServiceImpl implements UserService 

	@Autowired // 自动注入UserMapper
	UserMapper userMapper;

	@Override
	public User queryUserByUid(int uid) 
		return userMapper.getUserByUid(uid);
	

	@Override
	public User getUserByUid(int uid) 
		return userMapper.getUserByUid(uid);
	

	@Override
	public List<User> getAllUser() 
		return userMapper.getAllUser();
	

	@Override
	public List<User> queryByUname(String uname) 
		return userMapper.queryByUname(uname);
	

	@Override
	public int addUser(User user) 
		return userMapper.addUser(user);
	

	@Override
	public int deleteUser(int uid) 
		return userMapper.deleteUser(uid);
	

	@Override
	public int updateUser(User user) 
		return userMapper.updateUser(user);
	


userController.java

package cn.test.controller;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.test.po.User;
import cn.test.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController 
	@Autowired
	UserService userService;
	
	//根据用户编号查询用户信息,返回一个用户对象
	@RequestMapping("/queryUserByUid")
	public String queryUser(int uid,Model model) 
		User u = userService.queryUserByUid(uid);
		model.addAttribute("user", u);
		return "userinfo";
	
	
	/*
	 * //查询所有用户信息
	 * 
	 * @RequestMapping("/getAllUser") public String getAllUser(Model model) 
	 * List<User> u = userService.getAllUser(); model.addAttribute("ulist", u);
	 * return "userinfo"; 
	 */
	
	//分页查询
	@RequestMapping("/getAllUser")
	public String getAllUser(Model model,@RequestParam(defaultValue="1") int pageNum,
			@RequestParam(defaultValue="1") int pageSize) 
		PageHelper.startPage(pageNum, pageSize);
		List<User> u = userService.getAllUser();
		PageInfo<User> p = new PageInfo<User>(u);
		model.addAttribute("ulist", u);
		model.addAttribute("pageinfo", p);
		return "userinfo";
		
	
	
	//通过uname查询
	@RequestMapping("/queryByUname")
	public String queryUser(String uname,Model model)
		List<User> u = userService.queryByUname(uname);
		PageInfo<User> p = new PageInfo<User>(u);
		model.addAttribute("ulist", u);
		model.addAttribute("pageinfo", p);
	    return "userinfo";	
	
	
	//添加用户(跳转到添加用户界面)
	@RequestMapping("/toAddUser")
	public String toAddUser() 
		return "addUser";
	
	
	//执行添加用户功能
	@RequestMapping("/addUser")
	public String addUser(User user) 
		int t = userService.addUser(user);
		if(t>0)
			return "forward:getAllUser";
		else
			return "error";
	
	
	//删除指定uid的用户
	@RequestMapping("/deleteUser")
	public String deleteUser(int uid) 
		int t = userService.deleteUser(uid);
		if(t>0)
			return "forward:getAllUser";
		else
			return "error";
	
	
	//修改指定uid的用户
	@RequestMapping("/toUpdateUser")
	public String toUpdateUser(int uid,Model model) 
		User user = userService.queryUserByUid(uid);
		model.addAttribute("user", user);
		return "updateUser";
	
	
	//@RequestMapping("/updateUser")
	public String updateUser(User user) 
		int t = userService.updateUser(user);
		if(t>0)
			return "forward:getAllUser";
		else
			return "error";
	
	

userinfo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户</title>
</head>
<body>
<form method="post" action="$pageContext.request.contextPath/user/queryByUname ">
<input type="text" name="uname"/>
<input type="submit" value="提交"/>
<a href="$pageContext.request.contextPath/user/toAddUser">添加</a>
</form>

<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
以上是关于javaee配置文件的主要内容,如果未能解决你的问题,请参考以下文章

javaee配置文件

javaee配置文件

javaee配置文件

JavaEE开发环境配置

JavaEE配置工具

JavaEE配置工具