码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc:整合ssm(mybatis层)

时间:2020-05-02 23:05:17      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:res   防止   where   创建表   配置文件   cti   change   资源   his   

1、创建一个数据库并在里面创建表

技术图片

 

 

2、创建一个项目(Maven)

 

3、导入依赖:

<dependencies>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--servlrt-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <!--jsp-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!--jstl-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
    </dependencies>

 

4、静态资源导出问题:解决方案

<!--在bulid中配置resources,防止资源导出失败的问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

 

5、在resources目录下创建配置文件

技术图片

 

在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>
    <!--配置数据源,与spring整合以后,交给spring去做-->
    <typeAliases>
        <package name="pers.zhb.pojo"></package>
    </typeAliases>
    <mappers>
        <mapper class="pers.zhb.dao.TeacherMapper"></mapper>
    </mappers>
</configuration>

在未进行整合的时候,在这里要对数据源进行配置,但是在进行ssm整合的时候直接交给了spring容器进行管理

 

6、dao层

接口:

public interface TeacherMapper {
    int addTeacher(Teacher teacher);
    int deleteTeacherById(String teacherno);
    int updateTeacher(Teacher teacher);
    Teacher queryTeacherById(@Param("teacherno") String teacherno);
    List<Teacher> queryAllTeacher();
}

实现类:

<?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="pers.zhb.dao.TeacherMapper">
    <insert id="addTeacher" parameterType="teacher">
       INSERT INTO teacher VALUES(#{teacherno},#{tname},#{major},#{prof},#{department})
    </insert>

    <delete id="deleteTeacherById" parameterType="id">
       delete from teacher where teacherno=#{teacherno}
    </delete>

    <update id="updateTeacher" parameterType="teacher">
       update teacher
       set prof=#{prof}
       where teacherno=#{teacherno}
    </update>

    <select id="queryTeacherById" resultType="Teacher">
          select * from teacher where teacherno=#{teacherno}
    </select>

    <select id="queryAllTeacher" resultType="Teacher">
        select * from student
    </select>
</mapper>

 

7、service层

接口:

public interface TeacherService {
    void addTeacher(Teacher teacher);
    int deleteTeacherById(String teacherno);
    int updateTeacher(Teacher teacher);
    Teacher queryTeacherById(String teacherno);
    List<Teacher> queryAllTeacher();
}

实现类:

public class TeacherServiceImpl implements TeacherMapper {
    private TeacherMapper teacherMapper;

    public void setTeacherMapper(TeacherMapper teacherMapper){
        this.teacherMapper=teacherMapper;
    }

    public int addTeacher(Teacher teacher) {
        return teacherMapper.addTeacher(teacher);
    }

    public int deleteTeacherById(String teacherno) {
        return teacherMapper.deleteTeacherById(teacherno);
    }

    public int updateTeacher(Teacher teacher) {
        return teacherMapper.updateTeacher(teacher);
    }

    public Teacher queryTeacherById(String teacherno) {
        return teacherMapper.queryTeacherById(teacherno);
    }

    public List<Teacher> queryAllTeacher() {
        return teacherMapper.queryAllTeacher();
    }
}

 

springmvc:整合ssm(mybatis层)

标签:res   防止   where   创建表   配置文件   cti   change   资源   his   

原文地址:https://www.cnblogs.com/zhai1997/p/12819858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!