3.1 解决属性名和字段名不一致的问题
很明显我现在数据库中的字段和实体属性中的字段名是不一样的在实体属性中我是password在数据库中我的是psw
解决方法:
●起别名
<select id="getUserById" resultType="com.kuang.pojo.User">
select id,name,pwd as password from mybatis.user where id = #{id}
</select>
2 resultMap
结果集映射
<!--结果集映射-->
<resultMap id="UserMap" type="User">
<!--column数据库中的字段,property实体类中的属性-->
<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>
●resultMap 元素是 MyBatis 中最重要最强大的元素,设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了, 最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显式地用到他们。
3.2 日志
如果数据库操作,出现了异常,需要排错。就需要借助日志!
●LOG4J 【掌握】
●STDOUT_LOGGING 【掌握】
在Mybatis中具体使用那个一日志实现,在设置中设定!
STDOUT_LOGGING标准日志输出
在mybatis核心配置文件中,配置我们的日志!
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
3.3Log4j
什么是Log4j?
- Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件
- 我们也可以控制每一条日志的输出格式;
- 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
- 通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
- 先导入log4j的包
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
- log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/fan.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
- 配置log4j为日志的实现
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
简单使用
1: 在要使用Log4j 的类中,导入包 import org.apache.log4j.Logger;
2:. 日志对象,参数为当前类的class
-
static Logger logger = Logger.getLogger(UserMapperTest.class);
3:日志级别
logger.info("info:进入了testLog4j");
logger.debug("debug:进入了testLog4j");
logger.error("error:进入了testLog4j");
3.4使用Limit分页
使用Mybatis实现分页,核心SQL
接口
List<User> getUserByLimit(Map<String,Integer> map);
Mapper.xml
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
测试
public void getUserByList(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",0);
map.put("pageSize",8);
List<User> userList = mapper.getUserByLimit(map);
for (User user : userList) {
System.out.println(userList);
}
sqlSession.close();
}
3.5RowBounds分页
不再使用SQL实现分页
1:接口
List<User> getUserByRowBounds();
2:mapper.xml
<select id="getUserByRowBounds" resultMap="UserMap">
select * from user
</select>
3:测试
public void getRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//通过getRowBounds实现分页
List<User> userList =sqlSession.selectList("com.example.mapper.UserMapper.getRowBounds");
for (User user : userList) {
System.out.println(userList);
}
sqlSession.commit();
sqlSession.close();
}
4 注解
4.1注解开发
1:注解在接口上实现
public interface UserMapper {
@Select("select * from user")
List<User> getUser();
}
2:需要再核心配置文件中绑定接口!
<mappers>
<mapper class="com.example.mapper.UserMapper"/>
</mappers>
3:测试
package MyTest;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MyTest {
@Test
public void getUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层主要是反射
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User>userListL=userMapper.getUser();
for (User user : userListL) {
System.out.println(userListL);
}
sqlSession.commit();
sqlSession.close();
}
}
本质:反射机制实现
底层:动态代理!
Mybatis详细的执行流程![重点理解]
4.2CRUD
写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
// 方法存在多个参数,所有的参数前面必须加上 @Param("id")注解
@Select("select * from user where id = #{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{password})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{password} where id = #{id}")
int updateUser(User user);
@Delete("delete from user where id = #{uid}")
int deleteUser(@Param("uid") int id);
}
测试
package MyTest;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MyTest {
@Test
public void getUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层主要是反射
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User>userListL=userMapper.getUser();
for (User user : userListL) {
System.out.println(userListL);
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void getUserById(){
SqlSession sqlsession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
sqlsession.commit();
sqlsession.close();
}
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层主要应用反射原理
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(11,"罗汉金钟","450"));
sqlSession.close();
}
@Test
public void updataUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updataUser(new User(9,"绯色的子弹","092"));
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(11);
sqlSession.close();
}
}