1.namespace
namespace中的包名要和mapper接口的包名一致!
2.select
选择查询语句
id:就是对应的namespace中的方法名;
resultType:Sql语句执行的返回值!
parameterType:参数类型
增删改必须提交事务:sqlSession.commit();
1.编写接口
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
//insert一个用户;
int addUser(User user);
//修改用户
int updateUser(User user);
//删除用户
int deleteUser(int id);
2.编写对应的mapper中的Sql语句
<select id="getUserList" resultType="pojo.User">
select * from mybatis.user
select>
<select id="getUserById" parameterType="int" resultType="pojo.User">
select * from mybatis.user where id = #{id}
select>
<insert id="addUser" parameterType="pojo.User" >
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd});
insert>
<update id="updateUser" parameterType="pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id};
update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id= #{id};
delete>
3.测试
public void test(){
//1.获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//2.执行Sql--方式一:getMapper
UserMapper userDao = sqlSession.getMapper(UserMapper.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭SqlSession
sqlSession.close();
}
public void getUserById(){
SqlSession sqlSession1 = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession1.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession1.close();
}
3.Insert
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(5,"jim","123123"));
//提交事务
sqlSession.commit();
sqlSession.close();
}
4.Update
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(5,"jack2","123456"));
sqlSession.commit();
sqlSession.close();
}
5.Delete
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(5);
sqlSession.commit();
sqlSession.close();
}
万能的Map
假设我们的实体类或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!
int addUser2(Map<String,Object>map);
<insert id="addUser2" parameterType="map" >
insert into mybatis.user (id,name,pwd) values (#{userid},#{username},#{userpwd});
insert>
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid",5);
map.put("username","jack3");
map.put("userpwd","123123");
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
模糊查询
1.在Java代码执行的时候,传递通配符%%
List<User> userList = userDao.getUserList2("%李%");
select * from mybatis.user where name like #{values};
2.在sql拼接中使用通配符
select * from mybatis.user where name like "%"#{values}"%";
List<User> userList = userDao.getUserList2("李");
mybatis-config.xml
mybatis的配置文件包含了会深深影响MyBatis行为的设置和属性信息
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
可以直接引入外部文件
可以在其中增加一些属性配置
如果两个文件有同一个字段,优先使用外部配置文件的!
类型别名(typeAliases)
<typeAliases>
<typeAlias type="pojo.User" alias="user"/>
typeAliases>
扫描实体类的包,它的默认别名就是这个类的类名,首字母小写
<typeAliases>
<package name="pojo"/>
typeAliases>
实体类比较少的时候,使用第一种
实体类多的时候,使用第二种
第一种可以DIY别名,第二种不可以(可以使用注解@alias)
映射器
<mappers>
<mapper resource="dao/UserMapper.xml"/>
mappers>
作用域和生命周期
SqlSessionFactoryBuilder:一旦创建SqlSession就不在需要,一般设置为局部变量
SqlSessionFactory:可以理解为数据库连接池,一旦被创建就一直存在
SqlSession:可以理解为连接池中的请求,实例是不安全的线程,不能被共享,使用完毕就关闭。
属性名和字段名不一致问题
解决办法:起别名
<select id="getUserById" parameterType="int" resultType="user">
select id,name, pwd as password from mybatis.user where id =#{id}
select>
resultMap结果集映射
<resultMap id="UserMap" type="user">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
resultMap>
<select id="getUserById" resultMap="UserMap">
select * from mybatis.user where id = #{id}
select>