IDEA从0搭建SSM项目【图文】(二)——本地运行SSM增删改查

此篇是在系列一的基础上进行的SSM开发,当然如果有ssm基础直接看此篇也是ok的~
本文图文较多,但是每一步都是新手不可略过的,没有特别说明可以直接粘贴使用
【最好项目结构和命名也相同,否则新手自己debug很浪费时间,比如路径问题~】
~希望有耐心,最终运行效果如图

/登录页面:
在这里插入图片描述
登录后跳转到employee列表页
在这里插入图片描述
添加,编辑,删除弹出模态框并实现功能
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

好了,具体实现如下~

一. 数据库新建employee表,department表

employee表【注意,department_id不要设成外键,实际项目开发都不会设】
在这里插入图片描述
department表
在这里插入图片描述

二. 新建DTO类,Service类,Mapper类,Controller类

① 先展示最终目录结构,别怕,下面一步步进行搭建~
在这里插入图片描述
② DTO
Employee类

public class Employee {
    private String id;
    private String name;
    private String age;
    private Double salary;
    private Department department;

    public Employee() {
    }
    ...setter和getter方法自己写
}
public class Department {
    private String id;
    private String name;

    public Department() {
    }
    ...setter和getter方法自己写
}

③ Service接口和实现类
在这里插入图片描述

public interface EmployeeService {
    // List
    List<Employee> selectAll();

    // add
    void addEmployee(Employee employee);

    // update
    void updateEmployee(Employee employee);

    //select
    Employee selectById(String id);

    // delete
    void deleteEmployee(String id);
}
public interface DepartmentService {
    // List
    List<Department> selectAll();
}
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void addEmployee(Employee employee) {
        employeeMapper.addEmployee(employee);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateEmployee(employee);
    }

    @Override
    public Employee selectById(String id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public void deleteEmployee(String id) {
        employeeMapper.deleteEmployee(id);
    }
}
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

}

④ Mapper接口和Service接口一样【不过为了结构上的完整性,需要Service接口】
在这里插入图片描述

public interface EmployeeMapper {
    // list
    List<Employee> selectAll();

    // add
    void addEmployee(Employee employee);

    // update
    void updateEmployee(Employee employee);

    // select
    Employee selectById(String id);

    // delete
    void deleteEmployee(String id);
}
public interface DepartmentMapper {
    // List
    List<Department> selectAll();
}

⑤ Controller类
在这里插入图片描述

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/employees",method = RequestMethod.POST)
    @ResponseBody
    public List<Employee> getAllEmplyees(){
        List<Employee> employees=employeeService.selectAll();
        return employees;
    }

    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public String addEmployee(@RequestParam(value = "id",required = false) String id, Employee employee){
        if(id==null){
            employeeService.addEmployee(employee);
        }
        else{
            employeeService.updateEmployee(employee);
        }
        return "employee";
    }

    @RequestMapping(value = "/selectEmployee",method = RequestMethod.POST)
    @ResponseBody
    public Employee selectById(String id){
        Employee employee=employeeService.selectById(id);
        return employee;
    }

    @RequestMapping(value = "/deleteEmployee",method = RequestMethod.POST)
    public String deleteEmployee(String id){
        employeeService.deleteEmployee(id);
        return "employee";
    }
}
@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/departments",method = RequestMethod.POST)
    @ResponseBody
    public List<Department> getAllDepartment(){
        List<Department> departments=departmentService.selectAll();
        return departments;
    }
}
@Controller
public class LoginController {
    @RequestMapping("/loginAuth")
    public String login(@RequestParam("name") String name,@RequestParam("password") String password){
        if(name.equals("admin") && password.equals("admin")){
            return "employee";
        }
        return "redirect:/login.html";
    }
}

三. 配置文件spring,springmvc,mybatis,db.properties

在这里插入图片描述
spring配置:applicationContext.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"
       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.xsd">

    <!--扫描service和dto-->
    <context:component-scan base-package="com.hand.service.impl"/>
    <context:component-scan base-package="com.hand.service"/>
    <context:component-scan base-package="com.hand.dto"/>

    <!--spring+mybatis-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--配置数据源-->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--扫描mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hand.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>

springmvc配置: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"

       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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
">

    <!--扫描controller-->
    <context:component-scan base-package="com.hand.controller"/>

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

    <!--静态资源交给default-servlet处理-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
</beans>

mybatis配置: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>
    <mappers>
        <package name="com.hand.mapper"></package>
    </mappers>
</configuration>

数据源配置:db.properties【数据库,用户名,密码需替换】

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=utf8
jdbc.username=你的数据库用户名
jdbc.password=你的数据库密码

EmployeeMapper.xml,也就是sql语句

<?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="com.hand.mapper.EmployeeMapper">
    <resultMap id="employee" type="com.hand.dto.Employee">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
        <result column="salary" property="salary"></result>
        <association property="department" javaType="com.hand.dto.Department">
            <id column="depId" property="id"></id>
            <result column="depName" property="name"></result>
        </association>
    </resultMap>

    <delete id="deleteEmployee" parameterType="string">
        delete from employee where id=#{id}
    </delete>

    <select id="selectAll" resultMap="employee">
        SELECT
        employee.id,
        employee.name,
        employee.age,
        employee.salary,
        department.id AS depId,
        department.name AS depName
        FROM
        employee,department
        WHERE
        employee.department_id = department.id
    </select>
    <select id="selectById" resultMap="employee">
        SELECT
        employee.id,
        employee.name,
        employee.age,
        employee.salary,
        department.id AS depId,
        department.name AS depName
        FROM
        employee,department
        WHERE
        employee.department_id = department.id
        AND
        employee.id=#{id}
    </select>

    <insert id="addEmployee" parameterType="com.hand.dto.Employee">
        insert into employee(name,age,salary,department_id) values(#{name},#{age},#{salary},#{department.id})
    </insert>

    <update id="updateEmployee" parameterType="com.hand.dto.Employee">
        update employee set name=#{name},age=#{age},salary=#{salary},department_id=#{department.id} where id=#{id}
    </update>
</mapper>

DepartmentMapper.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="com.hand.mapper.DepartmentMapper">
    <resultMap id="department" type="com.hand.dto.Department">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
    </resultMap>

    <select id="selectAll" resultMap="department">
        select id,name from department
    </select>
</mapper>

到此,后端开发结束,接口全部写完,可以postman调用接口进行测试了~

四. 前端页面~不是本文重点,我直接把整个项目扔上来了

在这里插入图片描述

代码在这里:
https://download.csdn.net/download/g_y_x_/10715699

不着急的话可以留邮箱。。我看到会发给你的~

相关推荐
<p> <span style="color:#E53333;font-size:24px;"><strong>主要内容</strong></span> </p> <p> 掌握MyBatis的常用配置,MyBatis的Mapper映射文件的编写,包含select, update, insert和delete,MyBatis映射文件 </p> 之parameters和@Param注解,MyBatis映射文件的resultMap的使用,MyBatis映射之association(一对一)配置与使用,<br /> MyBatis映射之collection(一对多)配置与使用,MyBatis映射之association与collection的复杂使用(一对一与一对多同时使用<br /> ),MyBatis动态SQL之if,choose, when, otherwise,where,set,foreach,分页查询,主键回填,日志配置等技术;掌握<br /> SpringMVC的项目整合配置,@Controller,@RequestMapping,@Resource,@PathVariable,@ResponseBody,<br /> @ModelAttribute,@CookieValue,@Transactional等注解的使用,json数据传值,国际化,拦截器,权限控制,生成日志,文<br /> 件上传下载,日期格式转换等。<br /> <br /> <span style="font-size:24px;color:#E53333;"><strong>开发技术:</strong></span>java,jsp,mysql,MyBatis,jquery,ajax,json,springmvc<br /> <span style="font-size:24px;color:#E53333;"><strong>运行环境</strong>:</span>jdk1.7及以上版本,tomcat7.0及以上版本,mysql5.5及以上版本<br /> <p> <span style="font-size:24px;color:#E53333;"><strong>开发工具: </strong></span>本项目开发工具是Eclipse,也支持myEclipse,Intellij Idea等其他版本开发工具 </p> <p> <span style="font-size:24px;color:#E53333;"><strong>适用人群:</strong></span> </p> <p> 零基础开始讲解MyBatis,SpringMVC,通过项目实战达到精通各种技术的能力,包括权限控制 </p> <div> <br /> </div> <br /> <br />
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页