MyBatis 学习二之简单练习巩固
1、新建一个maven项目并在pom.xml中添加依赖
2、项目架构
配置文件:SqlMapConfig.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> <!-- 和spring整合后 environments配置将废除--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <package name="dongtian.TestMybatis.mapper"/> </mappers> </configuration>
POJO类
package dongtian.TestMybatis.pojo; import java.util.Date; public class User { private int id; private String username; private Date birthday; private String sex; private String address; 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address=" + address + "]"; } }
UserMapper接口
package dongtian.TestMybatis.mapper; import dongtian.TestMybatis.pojo.User; public interface UserMapper { public void insertUser(User role);
//@Select("select * from user where username = #{username}"):可以使用注解的形式
public User findUsers(String username); }
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="dongtian.TestMybatis.mapper.UserMapper"> <insert id="insertUser" parameterType="dongtian.TestMybatis.pojo.User"> insert into user(username,sex,address) values(#{username},#{sex},#{address}) </insert> <select id="findUsers" parameterType="String" resultType="dongtian.TestMybatis.pojo.User"> select * from user where username = #{username} </select> </mapper>
测试类
package dongtian.TestMybatis.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 org.junit.Before; import org.junit.Test; import dongtian.TestMybatis.mapper.UserMapper; import dongtian.TestMybatis.pojo.User; public class MapperTest { private SqlSessionFactory sqlSessionFactory; @Before public void setup() throws IOException { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void TestFindUsers() throws Exception{ SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper=sqlSession.getMapper(UserMapper.class); User user = userMapper.findUsers("张三"); System.out.println(user); } @Test public void TestInsertUser() throws Exception{ SqlSession sqlSession =null; try { sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper=sqlSession.getMapper(UserMapper.class); User user = new User(); user.setAddress("广东省广州市"); user.setUsername("林东"); user.setSex("男"); userMapper.insertUser(user); sqlSession.commit(); System.out.println(user); }catch(Exception e) { sqlSession.rollback(); }finally { if(sqlSession!=null) { sqlSession.close(); } } } }