pom.xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
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>
<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/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/feige/Mapper/UserMapper.xml"/>
</mappers>
</configuration>
POJO类
public class User {
private int id;
private String name;
private String hobby;
private int age;
......setter、getter省略
}
SqlSessionUtill工具类
public class SqlSessionUtill {
private final static Class<SqlSessionUtill> LOCK = SqlSessionUtill.class;
private static SqlSessionFactory sqlSessionFactory = null;
private SqlSessionUtill() {};
public static SqlSessionFactory getSqlSessionFactory() {
synchronized (LOCK) {
if(sqlSessionFactory != null) {
return sqlSessionFactory;
}
String resource = "mybatis_config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return sqlSessionFactory;
}
}
public static SqlSession openSqlSession() {
if(sqlSessionFactory == null) {
getSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
UserMapper.java
public interface UserMapper {
User findUserById(int id) throws Exception;
int deleteUserById(int id) throws Exception;
int addUser(User user) throws Exception;
int updateUser(User user) throws Exception;
}
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="com.feige.Mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="com.feige.po.User">
select * from user where id = #{id}
</select>
<delete id="deleteUserById" parameterType="java.lang.Integer" >
delete from user where id=#{value}
</delete>
<insert id="addUser" parameterType="com.feige.po.User" >
insert into user(id, name, hobby, age) value(#{id}, #{name}, #{hobby}, #{age})
</insert>
<update id="updateUser" parameterType="com.feige.po.User">
update user set name=#{name}, hobby=#{hobby}, age=#{age} where id=#{id}
</update>
</mapper>
注:当参数是一个基本数据类型时,要用#{value}。statement的id属性要和接口的方法名对应。
Test.java
public class Test {
public static void main(String[] args) throws Exception {
SqlSession session = null;
try {
session = SqlSessionUtill.openSqlSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = new User();
user.setId(2);
user.setAge(11);
user.setHobby("6666");
user.setName("feige");
int num = userMapper.updateUser(user);
System.out.println("影响数据的条数:"+num);
session.commit();
} finally {
session.close();
}
}
}
注:SqlSession默认是开启事务且不自动提交的,增删改默认返回影响数据的条数。
开发规范
1、Mapper.java和Mapper.xml在同一个包下。
2、Mapper.java中的方法名字和Mapper.xml中statement的id一样。
3、Mapper.xml中的namespace等于Mapper.java的全限命名。
4、Mapper.java接口方法中输入的参数类型,和Mapper.xml中statement的paramterType指定类型一致。
5、Mapper.java接口方法中返回的参数类型,和Mapper.xml中statement的resultType(resultMap的映射类型)指定类型一致。