SSM教程 (二) mybatis

断了今生、忘了曾经 提交于 2020-02-07 21:05:37

完整项目见  https://gitee.com/anti-murphy/demo_ssm

 

第一步:在resources目录下新建jdbc.properties文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/library_ssm?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=000000

第二步:在resources目录下的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>


    <properties resource="jdbc.properties"/>

    <typeAliases>
        <typeAlias alias="Book" type="cn.xyz.entity.Book"/>
        <typeAlias alias="User" type="cn.xyz.entity.User"/>
        <typeAlias alias="Admin" type="cn.xyz.entity.Admin"/>
        <typeAlias alias="Record" type="cn.xyz.entity.Record"/>
    </typeAliases>

    <!-- 环境,可以配置多个,default:指定采用哪个环境 -->
    <environments default="development">

        <!-- id:唯一标识 -->
        <environment id="development">
            <!-- 事务管理器,JDBC类型的事务管理器 -->
            <transactionManager type="JDBC" />
            <!-- 数据源,池类型的数据源 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>

    </environments>
    <mappers>
        <!--<mapper resource="cn/xyz/dao/mapper/BookMapper.xml"/>-->
        <package name="cn.xyz.dao.mapper"/>
    </mappers>


</configuration>
View Code

第三步:在entity目录下建立需要的dao

package cn.com.entity;

import org.springframework.stereotype.Component;

@Component
public class User {
    private int id;
    private String name;
    private String psd;
    private int level;


    public User() {
    }

    public User(String name, String psd) {
        this.name = name;
        this.psd = psd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", psd='" + psd + '\'' +
                ", level=" + level +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPsd() {
        return psd;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
}
View Code

第四步:在java.cn.com.dao.mapper下新建 接口UserMapper.java

public interface UserMapper {
    public List<User> queryUserAll();
    public User queryUserById(int id);
    public int insertUser(User user);
    public void deleteUser(int id);
    public void updateUser(User user);
}
View Code

第五步:在resources.cn.com.dao.mapper下新建接口对应的xml  如UserMapper.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="cn.xyz.dao.mapper.UserMapper">


    <select id="queryUserAll" resultType="User">
        select * from user
    </select>

    <select id="queryUserById" parameterType="int" resultType="User">
        select * from user where id = #{id}
    </select>

    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="User">
        insert into user(name,psd)
        value (#{name},#{psd})
    </insert>

    <delete id="deleteUser" parameterType="int">
        delete from user where id = #{id}
    </delete>

    <update id="updateUser" parameterType="User">
        update user set
        <if test="name!=null and name!=''">
            name = #{name},
        </if>
        <if test="psd!=null and psd!=''">
            psd=#{psd},
        </if>
        <if test="level!=null">
            level=#{level},
        </if>
        id=#{id}
        where id = #{id}
    </update>

</mapper>
View Code

第六步:在test目录下新建测试文件

 

 

package cn.xyz.dao.mapper;

import cn.xyz.entity.User;
import cn.xyz.util.SessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;


import static org.junit.Assert.*;

public class UserMapperTest {

    static Logger logger = Logger.getLogger(UserMapperTest.class);

    @Test
    public void queryUserAll() {
        try (SqlSession session = SessionFactory.getSession()){
            UserMapper userMapper = session.getMapper(UserMapper.class);
            List<User> users = userMapper.queryUserAll();
            Iterator<User> it  = users.iterator();
            while(it.hasNext()){
                User user = it.next();
                logger.info(user.toString());

            }
        }
    }
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!