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="mysql.properties"></properties>
<!-- 为JAVA Bean起类别名 -->
<typeAliases>
<typeAlias alias="User" type="com.xuxd.entity.User" />
</typeAliases>
<!-- 配置拦截器 -->
<plugins>
<plugin interceptor="com.xuxd.interceptor.BackCntsInterceptor">
</plugin>
</plugins>
<!-- 配置mybatis运行环境 -->
<environments default="mybatis">
<environment id="mybatis">
<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>
<mapper resource="mapper/UserDao.xml"></mapper>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
mysql.properties
#数据库连接信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/demodb?useUnicode=true&characterEncoding=UTF-8&useSSL=true
jdbc.username=root
jdbc.password=123456
使用Mapper接口方式与使用sqlSession传sqlID方式上面的配置信息都是一样的
使用Mapper接口方式如下:
/**
* Created by dong on 2016/12/15.
*/
@Repository("userDaoNoImpl")
public interface UserMapper {
public List<User> findUserByNameAge(@Param("username") String username1, @Param("age") int age);
public void saveUser(User user);
public void updatePassByName(@Param("username") String username, @Param("password") String password);
public void deleteByName(@Param("username") String username);
}
映射文件UserMapper.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xuxd.dao.UserMapper">
<sql id="user_column">
username,password,age
</sql>
<!-- 自定义返回结果集 -->
<resultMap id="User" type="User">
<result property="username" column="username" javaType="java.lang.String"></result>
<result property="password" column="password" javaType="java.lang.String"></result>
</resultMap>
<!--根据用户名和年龄查询-->
<select id="findUserByNameAge" resultMap="User">
select * from user1 where username=#{username} and age=#{age}
</select>
<!-- 增加用户 -->
<insert id="saveUser" parameterType="User">
insert into user1
(<include refid="user_column"/>)
values
(#{username},#{password},#{age})
</insert>
<!-- 通过用户名修改用户密码 -->
<update id="updatePassByName" parameterType="String">
update user1 set password=#{password} where username=#{username}
</update>
<!-- 通过用户名删除用户 -->
<delete id="deleteByName" parameterType="String">
delete from user1 where username=#{username}
</delete>
</mapper>
注意:【下面这几句总结参考:https://my.oschina.net/u/1757476/blog/500079】
Mapper 接口方法名 和 UserMapper.xml 中定义的每个 sql 的 id 同名。
Mapper 接口方法的输入参数类型和 UserMapper.xml 中定义的 sql parameterType 类型相同。
Mapper 接口的输出参数类型和 UserMapper.xml 中 定义的 sql 的 resultType 类型相同。
UserMapper.xml 文件中的 namespace 即是 Mapper 接口的类路径。
sqlSession传sqlID方式
/**
* Created by dong on 2016/12/15.
*/
public interface UserDao {
public List<User> getUserForList(String sqlId, User user);
public void saveUser(String sqlId, User user);
public void updatePassByName(String sqlId, User user);
public void deleteByName(String sqlId, User user);
}
/**
* Created by dong on 2017/1/13.
*/
@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {
private static final Logger log = LoggerFactory.getLogger(UserDao.class);
private SqlSession session = DBTools.getSession("mybatis-config.xml");
public List<User> getUserForList(String sqlId, User user) {
return session.selectList(sqlId, user);
}
public void saveUser(String sqlId, User user) {
session.insert(sqlId, user);
session.commit();
}
public void updatePassByName(String sqlId, User user) {
session.update(sqlId, user);
session.commit();
}
public void deleteByName(String sqlId, User user) {
session.delete(sqlId, user);
session.commit();
}
private static class DBTools {
private static SqlSession getSession(String resource) {
//使用MyBatis提供的Resources类加载mybatis的配置文件
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//session.getConfiguration().addMapper(UserDao.class);
return session;
}
private static void closeSession(SqlSession session) {
session.close();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xuxd.dao.UserDao">
<sql id="user_column">
username,password,age
</sql>
<!-- 自定义返回结果集 -->
<resultMap id="User" type="User">
<result property="username" column="username" javaType="java.lang.String"></result>
<result property="password" column="password" javaType="java.lang.String"></result>
</resultMap>
<!--根据用户名和年龄查询-->
<select id="getUserForList" resultMap="User">
select * from user1 where username=#{username} and age=#{age}
</select>
<!-- 增加用户 -->
<insert id="saveUser" parameterType="User" >
insert into user1
(<include refid="user_column"/>)
values
(#{username},#{password},#{age})
</insert>
<!-- 通过用户名修改用户密码 -->
<update id="updatePassByName" parameterType="User">
update user1 set password=#{password} where username=#{username}
</update>
<!-- 通过用户名删除用户 -->
<delete id="deleteByName" parameterType="User">
delete from user1 where username=#{username}
</delete>
</mapper>
这种方式增、删、改会自动开启事务,所以需要session.commit,查询不需要
sqlSession传sqlID这种方式mapper的namespace是可以很方便的自由修改的,但如果与接口类路径不同需要下面这句
session.getConfiguration().addMapper(UserDao.class);