JAVA系列笔记十三之Mybatis

1.添加依赖包

   <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>

2.主配置文件,目录在resources文件夹下

<?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>
<environments default="mysql">
    <environment id="mysql">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="drvier" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///mybatis001"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        </dataSource>
    </environment>
</environments>
    <mappers>
        <mapper resource="mappers/UserMapper.xml" />
    </mappers>
</configuration>

3. mapper配置文件

<?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 xmlns="http://mybatis.org/schema/mybatis-mapper" namespace="com.xiaoyui.mapper.UserMapper">
 <!--id:对应的接口的函数,resultType:为返回值,-->
 <select id="GetAll"  resultType="com.xiaoyui.domain.UserInfo"> select * from user;</select>
 <!--形参有两种形式#{}或者${}-->
 <select id="GetFindId"  resultType="com.xiaoyui.domain.UserInfo" parameterType="java.lang.Integer">
  select * from user where id=#{id};</select>
 <!--#{username}类的属性名称-->
 <insert id="Add" parameterType="com.xiaoyui.domain.UserInfo">
  <!--插入操作的返回值-->
  <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
    select last_insert_id();
  </selectKey>
   insert into user(username,sex) values(#{username},#{sex});
 </insert>
 <update id="Update" parameterType="com.xiaoyui.domain.UserInfo">
    update user set  username=#{username},sex=#{sex} where id=#{id};
 </update>
 <delete id="Delete" parameterType="java.lang.Integer">
    delete from user where id=#{id};
 </delete>
</mapper>

4.代码

bean

public class UserInfo  implements   Serializable{

    private  Integer id;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    private String username;
    private  String sex;
}

 Dao接口:

public interface UserMapper {

    public List<UserInfo> GetAll();

    public  UserInfo GetFindId(int id);

    public int Add(UserInfo user);

    public  int Update(UserInfo user);

    public  int Delete(int id);
}

测试代码如下:

  @Test
    public  void testUserGetAll() throws IOException
    {

        InputStream inputStream= Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session= sqlSessionFactory.openSession();


        UserMapper userMapper=session.getMapper(UserMapper.class);
        List<UserInfo> list= userMapper.GetAll();
        /*
        第二种获取实体对象的方式
         */
        //List<UserInfo> list= session.selectList("com.xiaoyui.mapper.UserMapper.GetAll");
        for (UserInfo user : list) {
            System.out.println(user.getUsername());
        }
        //获取单个
        UserInfo current=userMapper.GetFindId(1);

        //插入
        userMapper.Add(new UserInfo(){
        });
        //修改
        current.setUsername("test6");
        userMapper.Update(current);
        //删除
        userMapper.Delete(1);
        //提交事务
        session.commit();


        session.close();
        inputStream.close();
    }

 

上一篇:MyBatis框架一对一与一对多映射


下一篇:【uni app】坑