欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis基于xml的一个小例子

程序员文章站 2022-06-30 09:14:06
...

第一步创建maven工程:
mybatis基于xml的一个小例子
直接点下一步
mybatis基于xml的一个小例子
下一步点finish
mybatis基于xml的一个小例子
第2步:在pom.xml中导入坐标:

 <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

第三步:在java包下新建com.luckdog.dao和com.luckdog.domain
在domian包下新建Account实体类,User实体类:

public class Accout implements Serializable {
    private int id;
    private int uid;
    private double money;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getId() {
        return id;
    }

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

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Accout{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                ", user=" + user +
                '}';
    }
}
public class User implements Serializable {
    private Integer id;
    private  String username;
    private Date birthday;
    private String sex;
    private String address;
    private List<Accout> accounts;

    public List<Accout> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Accout> accounts) {
        this.accounts = accounts;
    }

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday='" + birthday + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

在dao包下建立IAccountDao

public interface IAccoutDao {
    /**
     * 查询所有
     * @return
     */
    List<Accout> findAll();

}

第四步在resources目录下新建com.luckdog.dao三层目录建立IAccountDao.xml文件,在resources目录下建立SqlMapConfig.xml文件
IAccountDao.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.luckdog.dao.IAccoutDao">
    <resultMap id="AccountUserMap" type="Accout">
        <id property="aid" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <association property="user" column="uid" javaType="user">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="address" column="address"></result>
            <result property="sex" column="sex"></result>
            <result property="birthday" column="birthday"></result>
        </association>
    </resultMap>
    <!--配置查询所有 -->
    <select id="findAll" resultMap="AccountUserMap">
        select u.*,a.id as aid,a.uid as uid,a.money as money from account a,user u where u.id=a.uid;
    </select>

</mapper>

SqlMapConfig.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">
<!-- mybits的主配置文件-->
<configuration>
    <typeAliases>
        <package name="com.luckdog.domain"></package>
    </typeAliases>
    <!--配置环境-->
    <environments default="mysql">
        <!--配置mysql环境-->
        <environment id="mysql">
            <!-- 配置事物类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源 (连接池)-->
            <dataSource type="POOLED">
                <!-- 配置连接数据库的四个基本信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>

        </environment>
    </environments>
    <!-- 指定映射配置文件的位置,映射配置文件是指每个dao独立独立的配置文件-->
    <mappers>
        <package name="com/luckdog/dao"></package>
    </mappers>
</configuration>

测试文件AccountTest:

public class AccoutTest {

    private InputStream in;
    private  SqlSessionFactoryBuilder builder;
    private SqlSessionFactory factory;
    private SqlSession sqlSession;
    private IAccoutDao accoutDao;
    @Before
    public void init()throws Exception{
        //1读取配置文件
        in =  Resources.class.getResourceAsStream("/SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(in);
        //3使用工厂生产SqlSession对象
        sqlSession=factory.openSession();
        //4使用SqlSession创建Dao接口代理对象
        accoutDao=sqlSession.getMapper(IAccoutDao.class);
    }
    //释放资源
    @After
    public void destroy() throws IOException {
        //提交事务
        sqlSession.commit();
        //6释放资源
        sqlSession.close();
        in.close();
    }
    @Test
    public void findAllAccout(){
        List<Accout> accouts=accoutDao.findAll();
        for(Accout accout:accouts){
            System.out.println(accout);
            System.out.println(accout.getUser());
        }
    }

}