基于SSM的健身房管理系统 (4)

3 userServiceImpl

public class UserServiceImpl implements UserService { private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public Date queryDateById(int id) { return userMapper.queryDateById(id); } public int updateNP(User user) { return userMapper.updateNP(user); } public User userLogin(User user) { return userMapper.userLogin(user); } public User queryUserById(int id) { return userMapper.queryUserById(id); } public int userRegister(User user) { return userMapper.userRegister(user); } }

4 adminServiceImpl

public class AdminServiceImpl implements AdminService { private AdminMapper adminMapper; public void setAdminMapper(AdminMapper adminMapper) { this.adminMapper = adminMapper; } public List<User> queryUser() { return adminMapper.queryUser(); } public int addUser(User user) { return adminMapper.addUser(user); } public int deleteUserById(int id) { return adminMapper.deleteUserById(id); } public int updateUser(User user) { return adminMapper.updateUser(user); } public User queryUserById(int id) { return adminMapper.queryUserById(id); } public User queryUserByName(String name) { return adminMapper.queryUserByName(name); } public Admin adminLogin(Admin admin) { return adminMapper.adminLogin(admin); } public int selectTotal() { return adminMapper.selectTotal(); } public List<User> findAll() { return adminMapper.findAll(); } public PageInfo<User> findByPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> list = adminMapper.findAll(); PageInfo<User> pageInfo = new PageInfo<>(list); return pageInfo; } }

Spring层

配置Spring整合MyBatis,我们这里数据源使用c3p0连接池;

编写Spring整合Mybatis的相关的配置文件;

spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置整合mybatis --> <!-- 1.关联数据库文件 --> <context:property-placeholder location="classpath:database.properties"/> <!-- 2.数据库连接池 --> <!--数据库连接池 dbcp 半自动化操作 不能自动连接 c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面) --> <bean> <!-- 配置连接池属性 --> <property value="${jdbc.driver}"/> <property value="${jdbc.url}"/> <property value="${jdbc.username}"/> <property value="${jdbc.password}"/> <!-- c3p0连接池的私有属性 --> <property value="30"/> <property value="10"/> <!-- 关闭连接后不自动commit --> <property value="false"/> <!-- 获取连接超时时间 --> <property value="10000"/> <!-- 当获取连接失败重试次数 --> <property value="2"/> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean> <!-- 注入数据库连接池 --> <property ref="dataSource"/> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property value="classpath:mybatis-config.xml"/> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 --> <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html--> <bean> <!-- 注入sqlSessionFactory --> <property value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property value="com.cc.dao"/> </bean> </beans>

Spring整合service层

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <!-- 扫描service相关的bean --> <context:component-scan base-package="com.cc.service" /> <!--AdminServiceImpl注入到IOC容器中--> <bean> <property ref="adminMapper"/> </bean> <!--UserServiceImpl注入到IOC容器中--> <bean> <property ref="userMapper"/> </bean> <!-- 配置事务管理器 --> <bean> <!-- 注入数据库连接池 --> <property ref="dataSource" /> </bean> </beans> SpringMVC层

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee " version="4.0"> <!--DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!--> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--encodingFilter--> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Session过期时间--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwspg.html