IDEA搭建Mybatis,从myeclipse过渡,初识IDEA必看!

由于IDEA越来越火,小编我也尝试了下IDEA,发现和eclipse,myeclipse差别还是有的,刚刚上手的时候有种陌生感,也百度了很多,才把mybatis搭建完成,并完成查询测试

1.xml配置
初始状态的IDEA是没有xml文件的,需要我们手动设置模板.

File->settings->IDEA搭建Mybatis,从myeclipse过渡,初识IDEA必看!在文本框中添加:

<?xml version="1.0" encoding="UTF-8" ?>

然后,就会在new 项目时 就会出现.xml了。

2.导入jar包
IDEA搭建Mybatis,从myeclipse过渡,初识IDEA必看!
3.代码
mybatis.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>
    <!--default引用environment的id,表示当前使用的环境-->
    <environments default="default">
        <!--声明可能使用的环境-->
        <environment id="default">
            <!--使用原生JDBC事务-->
            <transactionManager type="JDBC"></transactionManager>
            <!--数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="账户名"/>
                <property name="password" value="密码"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/wj/mapper/FlowerMapper.xml"/>
    </mappers>
</configuration>

FlowerMapper.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">
<!-- a.b 包名+类名-->
<mapper namespace="a.b">
        <select id="selAll" resultType="com.wj.pojo.Flower">
            select * from flower;
        </select>
</mapper>

在写完查询语句后,IDEA编译器必须在database中配置数据库连接参数.
IDEA搭建Mybatis,从myeclipse过渡,初识IDEA必看!

实体类:Flower

package com.wj.pojo;

public class Flower {
	private int id;
	private String name;
	private double price;
	private String production;
	public Flower(int id, String name, double price, String production) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.production = production;
	}
	public Flower() {
		super();
	}
	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 double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getProduction() {
		return production;
	}
	public void setProduction(String production) {
		this.production = production;
	}

	@Override
	public String toString() {
		return "Flower{" +
				"id=" + id +
				", name='" + name + '\'' +
				", price=" + price +
				", production='" + production + '\'' +
				'}';
	}

}

Test:测试程序

package com.wj.test;

import com.wj.pojo.Flower;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

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


public class Test {
    public static void main(String[] args) {
        try {
            //读取mybatis配置文件
            InputStream is = Resources.getResourceAsStream("mybatis.xml");
            //工厂设计模式
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
            //生产sqlSession
            SqlSession session=factory.openSession();
            List<Flower> list = session.selectList("a.b.selAll");
            for (Flower flower:list) {
                System.out.println(flower.toString());
            }
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这样,mybatis的搭建就算能完成了,并执行了查询语句。
另外由于篇幅限制,数据库建立小编这里就没有列出,请读者根据pojo实体类属性,进行数据库设计.
如果对你有所帮助,请给小编点个赞哦。有什么问题也可以在评论区留言,大家一起讨论。

上一篇:Analysis of the current development of CBB capacitors


下一篇:ruby-on-rails – 在域的子uri上部署rails app