从Controller->service->Impl->Mapper

本文介绍了一个基于SpringBoot的简单应用案例,利用MyBatis框架完成用户的增删查改操作。通过Controller、Service、ServiceImpl及Mapper等组件展示了如何进行数据层交互,并给出了具体的实现代码。
//Controller
@RestController
public class HelloController {    
@Autowired
private UserService userService;
@GetMapping("listnew")
    public List<User> listdemo(){
        return userService.listAll();
    }
}

//Service

@Service
public interface UserService extends IService<User> {
    List<User> listAll();
}

//ServiceImpl

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService{

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> listAll() {
        return userMapper.ListAll();
    }
}

//Mapper 使用了Mybatise 继承了BaseMapper

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {

    List<User> ListAll();//ListAll对应mapper文件中接口
}

//Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--绑定一个对应的Dao、Mapper接口-->
<mapper namespace="com.huang.wms.mapper.UserMapper">
    <!--    查询语句-->
    <select id="ListAll" resultType="com.huang.wms.entity.User">
        select * from wms_user;
    </select>
</mapper>
2025-10-23 16:04:19 DEBUG DispatcherServlet:865 - DispatcherServlet with name 'springMVC' processing GET request for [/user/list] 2025-10-23 16:04:19 DEBUG RequestMappingHandlerMapping:310 - Looking up handler method for path /user/list 2025-10-23 16:04:19 DEBUG RequestMappingHandlerMapping:317 - Returning handler method [public java.lang.String com.yourcompany.controller.UserController.getUserList(org.springframework.ui.Model)] 2025-10-23 16:04:19 DEBUG DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'userController' 2025-10-23 16:04:19 DEBUG DispatcherServlet:951 - Last-Modified value for [/user/list] is: -1 16:04:19.343 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 16:04:19.350 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4991572] was not registered for synchronization because synchronization is not active 2025-10-23 16:04:19 DEBUG DataSourceUtils:110 - Fetching JDBC Connection from DataSource 16:04:19.440 [http-nio-8080-exec-3] INFO com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge6yybe4gs89r1q2nsaf|10ed5253, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge6yybe4gs89r1q2nsaf|10ed5253, idleConnectionTestPeriod -> 300, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/as3?useUnicode=true&characterEncoding=UTF-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ] 16:04:19.457 [http-nio-8080-exec-3] DEBUG com.mchange.v2.cfg.MConfig - The configuration file for resource identifier '/mchange-commons.properties' could not be found. Skipping. 16:04:19.457 [http-nio-8080-exec-3] DEBUG com.mchange.v2.cfg.MConfig - The configuration file for resource identifier '/mchange-log.properties' could not be found. Skipping. 16:04:19.457 [http-nio-8080-exec-3] DEBUG com.mchange.v2.cfg.MConfig - The configuration file for resource identifier '/c3p0.properties' could not be found. Skipping. 16:04:19.457 [http-nio-8080-exec-3] DEBUG com.mchange.v2.cfg.MConfig - The configuration file for resource identifier 'hocon:/reference,/application,/c3p0,/' could not be found. Skipping. 16:04:19.457 [http-nio-8080-exec-3] WARN com.mchange.v2.resourcepool.BasicResourcePool - Bad pool size config, start 3 < min 5. Using 5 as start. 16:04:19.459 [http-nio-8080-exec-3] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - com.mchange.v2.resourcepool.BasicResourcePool@158df5a3 config: [start -> 5; min -> 5; max -> 20; inc -> 3; num_acq_attempts -> 30; acq_attempt_delay -> 1000; check_idle_resources_delay -> 300000; max_resource_age -> 0; max_idle_time -> 0; excess_max_idle_time -> 0; destroy_unreturned_resc_time -> 0; expiration_enforcement_delay -> 0; break_on_acquisition_failure -> false; debug_store_checkout_exceptions -> false; force_synchronous_checkins -> false] 16:04:19.459 [http-nio-8080-exec-3] DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPoolManager - Created new pool for auth, username (masked): 'ro******'. 16:04:19.459 [http-nio-8080-exec-3] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - acquire test -- pool size: 0; target_pool_size: 5; desired target? 1 16:04:19.459 [http-nio-8080-exec-3] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - awaitAvailable(): [unknown] 16:04:19.693 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@1be497fe [wrapping: com.mysql.jdbc.JDBC4Connection@48b0a4c5]] will not be managed by Spring 16:04:19.700 [http-nio-8080-exec-3] DEBUG com.yourcompany.dao.UserMapper.getAllUsers - ==> Preparing: SELECT * FROM user 16:04:19.724 [http-nio-8080-exec-3] DEBUG com.yourcompany.dao.UserMapper.getAllUsers - ==> Parameters: 16:04:19.750 [http-nio-8080-exec-3] DEBUG com.yourcompany.dao.UserMapper.getAllUsers - <== Total: 1 16:04:19.755 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4991572] 2025-10-23 16:04:19 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource [User{id=1, name='111', email=11111}]jgeydugwu 2025-10-23 16:04:19 DEBUG DefaultListableBeanFactory:1631 - Invoking afterPropertiesSet() on bean with name 'userList' 2025-10-23 16:04:19 DEBUG DispatcherServlet:1251 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'userList'; URL [/userList.html]] in DispatcherServlet with name 'springMVC' 2025-10-23 16:04:19 DEBUG JstlView:432 - Added model object 'user' of type [java.util.ArrayList] to request in view with name 'userList' 2025-10-23 16:04:19 DEBUG JstlView:166 - Forwarding to resource [/userList.html] in InternalResourceView 'userList' 2025-10-23 16:04:19 DEBUG DispatcherServlet:865 - DispatcherServlet with name 'springMVC' processing GET request for [/userList.html] 2025-10-23 16:04:19 DEBUG RequestMappingHandlerMapping:310 - Looking up handler method for path /userList.html 2025-10-23 16:04:19 DEBUG RequestMappingHandlerMapping:320 - Did not find handler method for [/userList.html] 2025-10-23 16:04:19 DEBUG SimpleUrlHandlerMapping:190 - Matching patterns for request [/userList.html] are [/**] 2025-10-23 16:04:19 DEBUG SimpleUrlHandlerMapping:219 - URI Template variables for request [/userList.html] are {} 2025-10-23 16:04:19 DEBUG SimpleUrlHandlerMapping:140 - Mapping [/userList.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/WEB-INF/views/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@246610]]] and 1 interceptor 2025-10-23 16:04:19 DEBUG DispatcherServlet:951 - Last-Modified value for [/userList.html] is: -1 2025-10-23 16:04:19 DEBUG DispatcherServlet:1044 - Null ModelAndView returned to DispatcherServlet with name 'springMVC': assuming HandlerAdapter completed request handling 2025-10-23 16:04:19 DEBUG DispatcherServlet:1000 - Successfully completed request 2025-10-23 16:04:19 DEBUG DispatcherServlet:1000 - Successfully completed request
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JiAngTxone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值