MyBatis使用笔记001

最简单的开发环境:mybatis-*.*.*.jar 和 jdbc驱动包。直接拷到lib文件夹下面。

下面是我的工程

我测试用的数据库

配置文件

mybatis-config.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>
	<properties resource="dbconfig.properties">
		<!--同一属性出现多次 ***.properties 优先读取  -->
		<property name="username" value="root"/>
		<property name="password" value="123456"/>
	</properties>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/mapper/UserInfoMapper.xml" />
	</mappers>
</configuration>

dbconfig.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=123456

UserInfoMapper.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.mapper.UserInfoMapper">

	<select id="getUserInfo" resultType="com.dto.UserInfo">
		select * from userinfo where userId = #{userId}
	</select>
	
	<insert id="insertUserInfo" parameterType="com.dto.UserInfo">
		insert into userinfo () values(#{userId},#{userPassword})
	</insert>
</mapper>

pojo  UserInfo.java

package com.dto;

public class UserInfo {

	private String userId;
	private String userPassword;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	@Override
	public String toString() {
		return "userId:"+userId+" userPassword:"+userPassword;
	}
}

测试——查找与插入

package com.test;

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;

import com.dto.UserInfo;

public class TestSelect {

	public static void main(String[] args) {
		UserInfo userInfo = new UserInfo();
		UserInfo userInfoInsert = new UserInfo();
		userInfoInsert.setUserId("xiaotu");
		userInfoInsert.setUserPassword("123456");
		int status=0;
		SqlSession session = null;
		String resource = "mybatis-config.xml";
		try {
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			session = sqlSessionFactory.openSession();
			userInfo = session.selectOne("com.mapper.UserInfoMapper.getUserInfo", "xiaolongnv");
			status=session.insert("com.mapper.UserInfoMapper.insertUserInfo", userInfoInsert);
			session.commit();
			//insert完一定要提交!!!!
			//UserInfoMapper userInfoMapper= session.getMapper(UserInfoMapper.class);
			//userInfo = userInfoMapper.getUserInfoByUserId(userId);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println(userInfo);
	}
}

这就是简单的,还没有整合spring。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值