7月6日 SSM 周五

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloworld_1996/article/details/80946727
<?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>
    <typeAliases>
        <package name="com.dto"/>
    </typeAliases>

    <environments default="管理员">
        <environment id="管理员">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///ssm01"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.mapper"/>
    </mappers>
</configuration>
<?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.mapper.DogMapper">
    <select id="findDogList" resultType="Dog">
        select * from t_dog
    </select>
    <select id="getDogObj" resultType="Dog">
        select * from t_dog where id = ${id}
    </select>
    <select id="getDogObj2" resultType="Dog">
        select * from t_dog where id = #{aa}
    </select>
    <select id="getCount" resultType="int">
        select count(*) from t_dog
    </select>
    <delete id="del">
        delete from t_dog where id = #{bb}
    </delete>
</mapper>
package com.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.dto.Dog;

public interface DogMapper {
    List<Dog> findDogList();
    Dog getDogObj(Dog d);
    Dog getDogObj2(@Param("aa")Integer id);
    Integer getCount();
    Integer del(@Param("bb")Integer id);
}
package com.demo;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtils {

    public static SqlSession getSqlSession() throws IOException{
        //加载mybatis-config.xml
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建一个SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
        //创建工厂
        SqlSessionFactory sqlSessionFactory = sfb.build(is);
        //打开SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
package com.demo;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.dto.Dog;
import com.mapper.DogMapper;

public class Demo {
    public static void main(String[] args) throws IOException {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        DogMapper mapper = sqlSession.getMapper(DogMapper.class);
        List<Dog> findDogList = mapper.findDogList();
//      for (Dog dog : findDogList) {
//          System.out.println(dog);
//      }
        Dog obj = mapper.getDogObj(new Dog(3,null,null,null));
        System.out.println(obj);
        Dog obj2 = mapper.getDogObj2(4);
        System.out.println(obj2);
        Integer count = mapper.getCount();
        System.out.println(count);
        Integer del = mapper.del(3);
        sqlSession.commit();
        sqlSession.close();
    }
}
package com.dto;

public class Dog {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    public Dog() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Dog(Integer id, String name, String sex, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Dog [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
                + age + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }


}

彻底的唯物主义者,是无所畏惧的。
——天下霸唱《鬼吹灯之寻龙诀》

阅读更多
换一批

没有更多推荐了,返回首页