最简单的开发环境: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。