jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123
beans.xml
b、 将SqlSessionFactroy工厂对象注入到Spring容器所管理
c、Mapper文件所对应的接口对象配置成bean管理
3、利用逆向工具生成pojo、mapper映射xml和接口,注意:需要在类前面加上@Repository注解
UserMapper.java
@Repository
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
4、编写一个业务bean,注意:需要在beans.xml扫描并在类前面加@ Service注解,依赖项userMapper前面加@Autowired自动配置bean的注解
UserService .java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int insertUser(User user) {
return userMapper.insert(user);
}
}
4、测试用例
MainTest.java
public class MainTest {
public static void main(String[] args)
{
//获取spring的IOC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService) ctx.getBean("userService");
User user=new User();
user.setName("张学友");
user.setAge(56);
user.setSex("男");
userService.insertUser(user);
}
}
2、在方法添加@Transactional注解
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Transactional
public int insertUser(User user) {
return userMapper.insert(user);
}
}
1、在web.xml文件
图解如下:
(1)ContextLoaderListener:上下文监听器,会生成上下文的root WebApplication(父IOC容器)
a、配置业务层的bean
day0110_ssm
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring/spring-service.xml,classpath:spring/spring-dao.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc-config.xml
1
springmvc
/
2、在springmvc-config.xml文件配置与springmvc核心组件相关的信息
3、在spring-service.xml文件配置数据源、业务层的bean、事务管理
4、在spring-dao.xml文件
整合mybatis框架
public class PageModel {
private int pageIndex;
private int pageSize=5;
private int totalRecordSum;
private int totalPageNum;
public int getPageIndex() {
this.pageIndex=this.pageIndex<=1?1:this.pageIndex;
this.pageIndex=this.pageIndex>=getTotalPageNum()?getTotalPageNum():this.pageIndex;
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecordSum() {
return totalRecordSum;
}
public void setTotalRecordSum(int totalRecordSum) {
this.totalRecordSum = totalRecordSum;
}
public int getTotalPageNum() {
if(getTotalRecordSum()==0) {
return 0;
}
this.totalPageNum = this.totalRecordSum%this.pageSize==0?this.totalRecordSum/this.pageSize: (this.totalRecordSum/this.pageSize)+1;
return totalPageNum;
}
public int getStartLimitPos() {
return (getPageIndex()-1)*getPageSize();
}
}
编写查询总记录sql定制方法
针对mysql的数据库的区域查询