用eclipse搭建mybatis环境(入门)

导入jar包到lib:

加入mybatis核心包,依赖包,数据驱动包,(junit-4.9.jar这个包是为了测试方便加入的)用eclipse搭建mybatis环境(入门)_第1张图片

在工程下面的src同级目录创建一个config目录:

用eclipse搭建mybatis环境(入门)_第2张图片
注意目录类型
用eclipse搭建mybatis环境(入门)_第3张图片

在config里面添加一个文件(File),里面类容如下:

//mybatis默认使用log4j作为输出日志信息.

Global logging configuration
log4j.rootLogger=DEBUG, stdout
Console output…
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

在config下创建SqlMapConfig.xml,如下:



DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
	<environments default="development">
		<environment id="development">
		
			<transactionManager type="JDBC" />
		
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/db_test1?characterEncoding=utf-8" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			dataSource>
		environment>
	environments>
configuration>

sql映射文件User.xml:


DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
mapper>

mybatis框架需要加载映射文件,将User.xml添加在SqlMapConfig.xml,如下:

<mappers>
		<mapper resource="sqlmap/User.xml"/>
mappers>

(别添加错了位置)添加后如下


DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
	<environments default="development">
		<environment id="development">
		
			<transactionManager type="JDBC" />
		
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/db_news?characterEncoding=utf-8" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			dataSource>
		environment>
	environments>

	
	<mappers>
		<mapper resource="User.xml"/>
	mappers>
	
	
	
	
configuration>

在User.xml配置根据id获取用户信息

<select id="findUserById" parameterType="int" resultType="com.yun.mybatis.domain.User">
		select * from user where id=#{id}
select>

parameterType:定义输入到sql中的映射类型,#{id}表示使用preparedstatement设置占位符号并将输入变量id传到sql。
resultType:定义结果映射类型。
在User.xml配置根据id获取用户信息(配置后)


DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
	
	<select id="findUserById" parameterType="int" resultType="com.yun.mybatis.domain.User">
			select * from user where id=#{id}
	select>
mapper>

创建一个USer类来测试一下:

public class User {
	private Integer id;
	private String username;
	private String password;
	private String email;
	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 getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
}

创建一个测试类Test:

public class Tset {

	SqlSessionFactory ssf = null;
	@Before
	public void creatFactory(){
		InputStream input = null;
			try {
				input = Resources.getResourceAsStream("SqlMapConfig.xml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		ssf = new SqlSessionFactoryBuilder().build(input);
	}
	/** 通过id查找*/
	@Test
	public void testById(){
		SqlSession session =ssf.openSession();
		User user = session.selectOne("test.testById", 8);
		System.out.println(user.getPassword());
		session.close();
	}

}

结果如下(我是通过id查找密码,所以这里显示的是密码):

用eclipse搭建mybatis环境(入门)_第4张图片

  • 到这里只是简单的入门,还在学习当中,也许当中也有错误,希望大佬指出谢谢!

你可能感兴趣的:(mybatis,java,数据库,mybatis)