准备
依赖
junit
junit
4.12
test
mysql
mysql-connector-java
5.1.34
org.mybatis
mybatis
3.2.7
org.bgee.log4jdbc-log4j2
log4jdbc-log4j2-jdbc4.1
1.16
org.slf4j
slf4j-api
1.7.13
org.slf4j
slf4j-log4j12
1.7.5
log4j
log4j
1.2.16
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig.xml
Mapper实现自增主键返回
select last_insert_id();
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
设置别名
Mappers映射器
MAVEN编译环境JDK版本插件
org.apache.maven.plugins
maven-compiler-plugin
1.8
UTF-8
动态SQL
IF
WHERE
SQL子句
id,username,birthday,sex,address
mybatis的关联查询
一:一对一关联查询
第一步,创建接口和映射文件
OrdersMapperQueryInterface接口定义
public interface OrdersMapperQueryInterface {
List queryOrdersJoinUsers();
}
UserMppaerQueryInterface接口定义
public interface UserMppaerQueryInterface();
OrdersMapperQueryInterface.xml定义
UserMppaerQueryInterface.xml定义
第二步:配置包扫描路径(SqlMapConfig.xml中配置)
第三步:接口中定义查询方法
List queryOrdersJoinUsers();
第四步:在orders中添加user属性
第五步:定义sql语句查询
第六步:测试用例开发
@Test
public void testName() throws Exception {
OrdersMapperQueryInterface mapper = sqlSession.getMapper(OrdersMapperQueryInterface.class);
List queryOrdersJoinUsers = mapper.queryOrdersJoinUsers();
for (Orders orders : queryOrdersJoinUsers) {
System.out.println(orders.getUser().getAddress());
System.out.println(orders.getUser().getUsername());
}
}
二:一对多关联查询
第一步:定义pojo类中的对应关系
第二步:定义接口中的方法
第三步:定义sql语句及返回值
Mapper代理形式开发dao
- 开发mapper接口
- 开发mapper文件
- 配置mapper代理
测试方法
public class UserMapperTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testGetUserById() {
UserMapper userMapper = applicationContext.getBean(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
}
}
扫描包形式配置mapper
每个mapper代理对象的id就是类名,首字母小写
动态代理
第一步:定义接口
public interface UserDao {
public void eat();
}
第二步:定义实现类
public class UserDaoImpl implements UserDao {
@Override
public void eat() {
System.out.println("我正在吃饭呢");
}
}
第三步:定义代理类
public class UserProxy implements InvocationHandler{
private UserDao userDao;
public UserProxy(UserDao userDao){
this.userDao = userDao;
}
public UserDao getUserDao(){
UserDao dao = (UserDao) Proxy.newProxyInstance(userDao.getClass().getClassLoader(), userDao.getClass().getInterfaces(), this);
return dao;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("eat")){
System.out.println("我先睡一觉");
return method.invoke(userDao, args);
}
return method.invoke(userDao, args);
}
}
第四步:测试用例
public class UserMain {
public static void main(String[] args) {
UserDao dao = new UserDaoImpl();
UserProxy proxy = new UserProxy(dao);
UserDao userDao = proxy.getUserDao();
userDao.eat();
}
}