该项目主要是完成Spring+SpringMVC+mybatis的完整整合,功能实现比较单一,就是一个完成增删改查的小项目!
完整代码下载地址SSM实战项目,人事管理系统源码+数据库+使用说明
1、管理员的登录,注册
2、员工的增删改查,批量删除
整个系统设计的目标人群是管理者,系统的主要功能是对员工进行各种信息的操作。
主要是完成对数据库的增删改查的功能。
分类 | 名称 | 语种 |
---|---|---|
操作系统 | windows10 | 简体中文 |
数据库平台 | MySQL Server 8.0+ | |
应用服务器 | apache-tomcat-8.5.71 | |
java开发工具 | idea | |
框架 | mybatis+Spring+SpringMVC | |
项目名称 | 《学生教务系统》 | |
实现技术 | mybatis+Spring+SpringMVC+mysql+Servlet+jquery+bootStrap+js+Maven+tomcat等技术 |
-- 创建员工表
create table t_emp(
id int primary key auto_increment,
name varchar(20) not null,
salary double not null,
age int not null
)
-- 添加员工数据
insert into t_emp values(null,'王恒杰',20000,21);
insert into t_emp values(null,'杨福君',9000,19);
-- 查询员工数据
select * from t_emp;
-- 创建管理员表
create table t_admin(
id int primary key auto_increment,
username varchar(20),
password varchar(50)
)
-- 添加数据
insert into t_admin values(null,'王恒杰','123456');
-- 查询
select * from t_admin
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.2.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<context:component-scan base-package="com.tjcu.whj">context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean class="com.alibaba.druid.pool.DruidDataSource" name="dataSource">
<property name="driverClassName" value="${jdbc.driver}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations" value="classpath:com/tjcu/mapper/*DaoMapper.xml">property>
<property name="typeAliasesPackage" value="com.tjcu.whj.entity">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
<property name="basePackage" value="com.tjcu.whj.dao">property>
bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>
功能模块:登录,注册,注销,密码加密
注册示意图
登录示意图
public interface AdminDao {
/**
* 登录
* @param admin
* @return
*/
public Admin login(Admin admin);
/**
* 注册
* @param admin
*/
public void register(Admin admin);
}
public interface AdminService {
/**
* 登录
* @param admin
* @return
*/
public Admin login(Admin admin);
/**
* 注册
* @param admin
*/
public void register(Admin admin);
}
@Service("adminService")
@Transactional
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminDao adminDao;
@Override
public Admin login(Admin admin) {
return adminDao.login(admin);
}
@Override
public void register(Admin admin) {
adminDao.register(admin);
}
}
public class AdminTest {
@Test
public void login(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AdminService adminService = (AdminService) context.getBean("adminService");
Admin admin = new Admin(null,null, "王恒杰", "123456",true);
Admin login = adminService.login(admin);
System.out.println(login);
}
@Test
public void register(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AdminService adminService = (AdminService) context.getBean("adminService");
Admin admin = new Admin(null, "风犬少年","邓正武", "234567",true);
adminService.register(admin);
}
}
@Controller("adminController")
@RequestMapping("admin")
public class AdminController {
/**
* 将adminService到AdminController中
*/
@Autowired
private AdminService adminService;
/**
* 登录
* @param admin
* @return
*/
@RequestMapping("login")
public String login(Admin admin,HttpServletRequest request){
String password = MD5Utils.md5(admin.getPassword());
admin.setPassword(password);
Admin admin1 = adminService.login(admin);
System.out.println(admin1);
if(admin1!=null){
request.getSession().setAttribute("admin",admin1);
return "redirect:/emp/show";
}
return "redirect:/login.jsp";
}
/**
* 注册
* @param admin
*/
@RequestMapping("register")
public String register(Admin admin){
String password = MD5Utils.md5(admin.getPassword());
admin.setPassword(password);
adminService.register(admin);
return "redirect:/login.jsp";
}
/**
* 注销登录
* @return
*/
@RequestMapping("destroy")
public String destroy(HttpServletRequest request){
request.getSession().invalidate();
return "redirect:/login.jsp";
}
}
员工的增删改查功能
员工展示页面
添加员工示意图
修改员工示意图
public interface EmpDao {
/**
* 添加员工
*
* @param emp
*/
public void insert(Emp emp);
/**
* 删除员工
* @param id
*/
public void deleteById(Integer id);
/**
* 展示员工
* @return
*/
public List<Emp> showEmp();
/**
* 修改员工
* @param emp
*/
public void updateEmp(Emp emp);
/**
* 数据回显
* @param id
* @return
*/
public Emp selectEmpById(Integer id);
}
public interface EmpService {
/**
* 添加员工
*
* @param emp
*/
public void insert(Emp emp);
/**
* 删除员工
* @param id
*/
public void deleteById(Integer id);
/**
* 展示员工
* @return
*/
public List<Emp> showEmp();
/**
* 修改员工
* @param emp
*/
public void updateEmp(Emp emp);
/**
* 数据回显
* @param id
* @return
*/
public Emp selectEmpById(Integer id);
}
@Service("empService")
/**
* 控制事务
*/
@Transactional
public class EmpServiceImpl implements EmpService {
/**
* 将empDao注入进@Service组件中
*/
@Autowired
private EmpDao empDao;
public void setEmpDao(EmpDao empDao) {
this.empDao = empDao;
}
@Override
public void insert(Emp emp) {
empDao.insert(emp);
}
@Override
public void deleteById(Integer id) {
empDao.deleteById(id);
}
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<Emp> showEmp() {
return empDao.showEmp();
}
@Override
public void updateEmp(Emp emp) {
empDao.updateEmp(emp);
}
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public Emp selectEmpById(Integer id) {
return empDao.selectEmpById(id);
}
}
public class EmpTest {
/**
* 添加员工
*/
@Test
public void insert(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService empService = (EmpService) context.getBean("empService");
Emp emp = new Emp(null,"邓正武",2000d,22);
empService.insert(emp);
}
/**
* 删除员工
*/
@Test
public void deleteById(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService empService = (EmpService) context.getBean("empService");
empService.deleteById(4);
}
/**
* 展示员工
* @return
*/
@Test
public void showEmp(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService empService = (EmpService) context.getBean("empService");
List<Emp> emps = empService.showEmp();
for (Emp emp : emps) {
System.out.println(emp);
}
}
/**
* 修改员工
*/
@Test
public void updateEmp(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService empService = (EmpService) context.getBean("empService");
Emp emp = new Emp(3,"邓正武",38000d,23);
empService.updateEmp(emp);
}
/**
* 数据回显
* @return
*/
@Test
public void selectEmpById(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService empService = (EmpService) context.getBean("empService");
Emp emp = empService.selectEmpById(1);
System.out.println(emp);
}
}
@Controller("emoController")
@RequestMapping("emp")
public class EmpController {
/**
* 注入empService在EmpController中
*/
@Autowired
private EmpService empService;
/**
* 添加员工
*
* @param emp
*/
@RequestMapping("insert")
public String insert(Emp emp){
empService.insert(emp);
return "redirect:/emp/show";
}
/**
* 删除员工
* @param emp
*/
@RequestMapping("delete")
public String deleteById(Emp emp){
empService.deleteById(emp.getId());
return "redirect:/emp/show";
}
/**
* 展示员工
* @return
*/
@RequestMapping("show")
public String showEmp(Model model){
//调用业务方法
List<Emp> emps = empService.showEmp();
//作用域
model.addAttribute("emps",emps);
return "emplist";
}
/**
* 修改员工
* @param emp
*/
@RequestMapping("update")
public String updateEmp(Emp emp){
empService.updateEmp(emp);
return "redirect:/emp/show";
}
/**
* 数据回显
* @param id
* @return
*/
@RequestMapping("select")
public String selectEmpById(Integer id,Model model){
Emp emp = empService.selectEmpById(id);
model.addAttribute("emp",emp);
return "updateEmp";
}
}
完整代码下载地址SSM实战项目,人事管理系统源码+数据库+使用说明