1、项目的结构
2、配置文件
2、1 jdbc.properties
jdbc.driverName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dms?useUnicode=true&characterEncoding=gbk
jdbc.user=root
jdbc.password=root
2、2 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 别名定义 -->
<typeAliases>
<!-- 批量别名定义,指定包名,mybatis自动扫描包中的po类,自动定义别名,别名是类名(首字母大写或小写都可以,一般用小写) -->
<package name="com.model"/>
</typeAliases>
</configuration>
2、3 spring-mvc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.*" />
<!-- 开启注解 -->
<mvc:annotation-driven />
<!--
配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
-->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
2、4 spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
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">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 文件映射器,指定类文件 -->
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:com/mapper/*Mapper.xml"/>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 通过构造函数注入 -->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置mybatis mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"/>
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
</bean>
<!--
4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5. 使用声明式事务
transaction-manager:引用上面定义的事务管理器
-->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
3、UserMapper.java
package com.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.model.User;
@Repository
public interface UserMapper {
List<User> findAllUser();
}
UserMapper.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
<!-- 解决表名与字段不匹配 -->
<resultMap type="User" id="userResultMap">
<id property="id" column="id"/>
<result property="type" column="type"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="status" column="status"/>
<result property="areaid" column="areaid"/>
<result property="employeeno" column="employeeno"/>
</resultMap>
<select id="findAllUser" resultMap="userResultMap" resultType="User">
select * from user
</select>
</mapper>
4、User.java
package com.model;
public class User {
public int id;//用户id
public String type;//用户类型普通员工 ce,管理员 sa,区域管理人员(进销存) stock 001,sell 002,save 003,
public String username;//用户名
public String password;//密码
public String status;//账号的使用状态 0 停用 1 使用
public int areaid;//用户所属区域
public String employeeno;//用户的工号
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getAreaid() {
return areaid;
}
public void setAreaid(int areaid) {
this.areaid = areaid;
}
public String getEmployeeno() {
return employeeno;
}
public void setEmployeeno(String employeeno) {
this.employeeno = employeeno;
}
}
5、UserService.java
package com.service;
import java.util.List;
import com.model.User;
public interface UserService {
List<User> findAllUser();
}
UserServiceImpl.java
package com.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mapper.UserMapper;
import com.model.User;
@Service(value="userService")
@Transactional
public class UserServiceImpl implements UserService{
@Resource
public UserMapper userMapper;
@Override
public List<User> findAllUser() {
// TODO Auto-generated method stub
List<User> findAllUsers = userMapper.findAllUser();
return findAllUsers;
}
}
6、UserController.java
package com.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.User;
import com.service.UserService;
@Controller
@RequestMapping("com")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAllUser")
public String findAllUser(HttpServletRequest request){
List<User> listUsers = userService.findAllUser();
request.setAttribute("user", listUsers.get(0));
return "/showRole";
}
}
7、jsp 页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div>用户名:${user.username}</div>
</body>
</html>
8、运行结果
9、表结构
create table user(id int primary key auto_increment,
type varchar(255) not null ,
username varchar(255) not null unique,
password varchar(255) not null,
sataus varchar(255) not null,
areaid int not null,
employeeno varchar(255) );
数据:
10、jar包
链接:https://pan.baidu.com/s/1v1sfx2o3zQpxb9y3szKcfQ 提取码:kl5z
12、项目