package org.westos.dao;
import org.westos.pojo.User;
import javax.jws.soap.SOAPBinding;
import java.util.List;
public interface UserMapper {
//添加一个用户
int addUser(User user);
//根据id查找用户
User selectUser(int id);
//根据id删除一个用户
int deleteUser(int id);
//修改用户信息
int update(User user);
}
<mapper namespace="org.westos.dao.UserMapper">
<insert id="addUser" parameterType="org.westos.pojo.User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
insert>
<select id="selectUser" resultType="org.westos.pojo.User">
select * from user where id=#{id}
select>
<delete id="deleteUser">
delete from user where id = #{id}
delete>
<update id="update" parameterType="org.westos.pojo.User">
update user set name = #{name},pwd = #{pwd} where id=#{id}
update>
mapper>
package org.westos.dao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.westos.pojo.User;
import org.westos.utils.MybatisUtils;
import java.sql.ParameterMetaData;
import java.util.Date;
public class UserMapperTest {
@Test
public void testAddUser(){
User user = new User(5, "元芳", "11111");
//1.拿到sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
//2.通过sqlSessionFactory对象openSession()创建一个sqlSession。
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.通过sqlSession获得mapper对象 , 参数为映射文件对应的接口类的class对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(user);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testSelectUserById(){
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectUser(2);
System.out.println(user.toString());
}
@Test
public void testDeleteUser(){
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testUpdateUser(){
User user = new User(1, "小学生", "123456");
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.update(user);
sqlSession.commit();
sqlSession.close();
}
}
configuration 配置
properties 配置文件
settings mybatis设置
typeAliases 为Java类起别名
typeHandlers 类处理器
objectFactory 对象工厂
plugins 插件
environments 环境
transactionManager 事务管理
dataSource : 数据源
mappers 映射器
在resources目录下创建database.properties
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8'
username = root
password = 123456
在mybatis核心配置文件中引入properties配置文件,但是properties标签的位置必须在配置文件中的最上面。
<configuration>
<properties resource="database.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="org/westos/dao/userMapper.xml"/>
mappers>
configuration>
在配置文件中为pojo中的实体类设置别名
typeAliases的alias属性为别名,type属性为包名+类名
也可指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
<package name="org.westos.pojo"/>
配置完毕可以在接口的映射中使用别名
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
MyBatis 可以配置成适应多种环境,这种机制有助于将 SQL 映射应用于多种数据库之中, 现实情况下有多种理由需要这么做。例如,开发、测试和生产环境需要有不同的配置;或者想在具有相同 Schema 的多个生产数据库中 使用相同的 SQL 映射。有许多类似的使用场景。
不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。
Mybatis默认的事务管理器是JDBC(另外一个MANAGED几乎没用),默认数据源是POOLED (其他的UNPOOLED和JNDI不常用)。
所以,如果你想连接两个数据库,就需要创建两个 SqlSessionFactory 实例,每个数据库对应一个
<environments default="development1">
<environment id="development1">
<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>
<environment id="development2">
<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>
resource 【推荐】
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
<mapper resource="org/mybatis/builder/BlogMapper.xml"/>
<mapper resource="org/mybatis/builder/PostMapper.xml"/>
mappers>
url
<mappers>
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
<mapper url="file:///var/mappers/BlogMapper.xml"/>
<mapper url="file:///var/mappers/PostMapper.xml"/>
mappers>
class
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
<mapper class="org.mybatis.builder.BlogMapper"/>
<mapper class="org.mybatis.builder.PostMapper"/>
mappers>
package
<mappers>
<package name="org.mybatis.builder"/>
mappers>