SSM 人事管理系统---> 整理

最近刚用ssm写完 人事管理 系统,记录一下自己的思路和心得


1.导入jar包

2.加入配置文件

applicationContext.xml   springmvc-config.xml

2.1 web.xml

2.11 

配置Spring核心监听器,默认会以 /WEB-INF/applicationContext.xml作为配置文件

org.springframework.web.context.ContextLoaderListener

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext*.xml</param-value>
	</context-param>
若要指定 applicationContext.xml文件的位置 可把<param-value>的值换成 classpath:applicationContext.xml
2.12

配置 springMVC前端控制器

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc-config.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>


2.13 可以加入 编码过滤器

 

  <filter>
		<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

3 applicationContext.xml

3.1 引入mapper类----->dao         

<mybatis:scan base-package="cn.xw.hrm.dao"/> 

3.2 把用到spring注解的类,加入扫描包,注册成 bean

<context:component-scan base-package="cn.xw.hrm"/>

3.3 加载数据源

<context:property-override location="classpath:db.properties"/>


3.3 用c3p0连接数据圆

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>

3.4 配置sessionFactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
   p:dataSource-ref="dataSource"/>

3.5 jdbc事务管理器

<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>

3.6 用注解方式管理事物

<tx:annotation-driven transaction-manager="transactionManager"/>


4 Springmvc-config.xml

扫描controller

    <context:component-scan base-package="cn.xw.hrm.controller"/>
    <!-- 设置默认配置方案 -->
    <mvc:annotation-driven />
    <!-- 使用默认的Servlet来响应静态文件 -->
    <mvc:default-servlet-handler/>
<!-- 
    	定义Spring MVC的拦截器 
    -->
    <mvc:interceptors>
    	<mvc:interceptor>
    		<mvc:mapping path="/*"/>
    	 	<bean class="cn.xw.hrm.interceptor.AuthorizedInterceptor"/>
    	</mvc:interceptor>
    </mvc:interceptors>

5 实体类

User   ------> set get方法

6.Dao------>mapper

public interface  userDao (){}

6.1 动态 sql

@SelectProvider(type=UserSqlProvide.class,method="updateUser")
void update(User user);

6.2 创建  class UserSqlProvide 

public String updateUser(final User user){
		return new SQL(){
			{
				UPDATE("user_inf");
				if(user.getUsername() != null){
					SET(" username = #{username} ");
				}
				if(user.getLoginname() != null){
					SET(" loginname = #{loginname} ");
				}
				if(user.getPassword()!= null){
					SET(" password = #{password} ");
				}
				if(user.getStatus()!= null){
					SET(" status = #{status} ");
				}
				if(user.getCreateDate()!= null){
					SET(" create_date = #{createDate} ");
				}
				WHERE(" id = #{id} ");
			}
		}.toString();
	}

7     


  interface  UserService (){}

根据id 修改 user

@Service("hrmService")

void updateUser(User user)

7.2 class UserService implements UserService (){

//自动注入dao
@Autowired
private UserDao userDao;

public void updateUser(User user){

userDao.update(user);

}

}


8----> 控制器

@Controller

class  UserController(){

@Autowired
@Qualifier("userService")
private UserService userService;

@RequestMapping("/user/updateUser")
public ModelAndView updateUser(
@ModelAttribute User user,
ModelAndView mv){

User user = userService.updateUser(user);

mv.addObject("user",user);

mv.setViemName("/user/selectUser");

retuen mv;

}

展开阅读全文

没有更多推荐了,返回首页