mybatis配置文件
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="db.properties"></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="mybatis/userMapper.xml"/>
</mappers>
</configuration>
web.xml不需要配置
1.首先建立一个实体类User
package mybatis;
public class User {
public int id;
public String userName;
public String userPwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + "]";
}
}
3、编写mybatis的mapper文件,文件名位对应的实体类名+mapper(类名的首字母小写)
userMapper.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="mybatis.UserMapper">
<select id="selectUser" resultType="mybatis.User" parameterType="mybatis.User">
select * from User where id = #{id}
</select>
<select id="selectALL" resultType="mybatis.User">
select * from user
</select>
<insert id="insertUser" parameterType="mybatis.User">
insert into user (userName,userPwd) values (#{userName},#{userPwd})
</insert>
<update id="updateUser" parameterType="mybatis.User">
update user set userName = #{userName},userPwd = #{userPwd} where id = #{id}
</update>
<delete id="deleteUser" parameterType="mybatis.User">
delete from user where id = #{id}
</delete>
</mapper>
下面为测试增删改查的测试代码。
package net.xinqushi.test;
import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import mybatis.User;
import net.xinqushi.utils.MybatisUtils;
public class Test3 {
@Test
public void testInsert() throws IOException {
SqlSession session = MybatisUtils.openSession();
String string = "mybatis.UserMapper.insertUser";
User user = new User();
user.setUserName("王建");
user.setUserPwd("王建");
session.insert(string, user);
session.commit();
session.close();
//System.out.println(count);
}
@Test
public void testDelete() throws IOException {
SqlSession sqlSession = MybatisUtils.openSession();
String statment = "mybatis.UserMapper.deleteUser";
User user = new User();
user.setId(20);
sqlSession.delete(statment, user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testUpdate() throws IOException {
SqlSession sqlSession = MybatisUtils.openSession();
String statment="mybatis.UserMapper.updateUser";
User user = new User();
user.setUserName("哈喽");
user.setUserPwd("8899");
user.setId(8);
sqlSession.update(statment, user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testSelect() throws IOException {
SqlSession session = MybatisUtils.openSession();
String string = "mybatis.UserMapper.selectUser";
User user=session.selectOne(string,1);
session.close();
System.out.println(user);
}
@Test
public void testSelectAll() throws IOException {
SqlSession sqlSession = MybatisUtils.openSession();
String statment="mybatis.UserMapper.selectALL";
List<User> list =sqlSession.selectList(statment);
System.out.println(list);
sqlSession.close();
}
}
5、最后在附上mybatis用来获取session的实体类的代码。
package net.xinqushi.utils;
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;
public class MybatisUtils {
public static SqlSession openSession() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"development");
return sqlSessionFactory.openSession();
}
}