国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構

            手機站
            千鋒教育

            千鋒學習站 | 隨時隨地免費學

            千鋒教育

            掃一掃進入千鋒手機站

            領取全套視頻
            千鋒教育

            關注千鋒學習站小程序
            隨時隨地免費學習課程

            當前位置:首頁  >  技術干貨  > springboot如何整合mybatis?

            springboot如何整合mybatis?

            來源:千鋒教育
            發(fā)布人:yyy
            時間: 2023-06-25 13:31:00 1687671060

              要在 Spring Boot 中整合 MyBatis,可以按照以下步驟進行操作:

              1. 添加 MyBatis 依賴:在 Maven 或 Gradle 構建文件中添加 MyBatis 的依賴。

              對于 Maven:

            <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>

               對于 Gradle: 

            implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter'

               2. 配置數(shù)據(jù)庫連接:在配置文件(如 application.properties 或 application.yml)中配置數(shù)據(jù)庫連接信息。 

            spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
            spring.datasource.username=username
            spring.datasource.password=password
            spring.datasource.driver-class-name=com.mysql.jdbc.Driver

               這里的 `url` 是數(shù)據(jù)庫的連接地址,`username` 和 `password` 是數(shù)據(jù)庫的用戶名和密碼,`driver-class-name` 是數(shù)據(jù)庫驅(qū)動的類名。

              3. 創(chuàng)建實體類和數(shù)據(jù)訪問對象(DAO):根據(jù)數(shù)據(jù)庫表結(jié)構創(chuàng)建相應的實體類和 DAO 接口。

            // 實體類
            public class User {
            private Long id;
            private String name;
            // getters and setters
            }
            // DAO 接口
            public interface UserDao {
            User getUserById(Long id);
            List<User> getAllUsers();
            void insertUser(User user);
            void updateUser(User user);
            void deleteUser(Long id);
            }

               4. 創(chuàng)建 MyBatis 映射文件:在 resources 目錄下創(chuàng)建 MyBatis 映射文件,用于定義 SQL 查詢和映射關系。

            <!-- userMapper.xml -->
            <mapper namespace="com.example.dao.UserDao">
            <select id="getUserById" resultType="com.example.model.User">
            SELECT * FROM users WHERE id = #{id}
            </select>
            <!-- 其他 SQL 語句 -->
            </mapper>

               5. 創(chuàng)建 MyBatis 配置文件:創(chuàng)建 MyBatis 的配置文件,配置 MyBatis 的相關設置。

            <!-- mybatis-config.xml -->
            <configuration>
            <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            </settings>
            </configuration>

               在上述示例中,配置了將數(shù)據(jù)庫表的下劃線命名轉(zhuǎn)換為駝峰命名。

              6. 創(chuàng)建 DAO 實現(xiàn)類:實現(xiàn) DAO 接口,并使用 MyBatis 的注解或 XML 配置將 SQL 語句與 DAO 方法綁定。

            @Repository
            public class UserDaoImpl implements UserDao {
            private final SqlSession sqlSession;
            public UserDaoImpl(SqlSession sqlSession) {
            this.sqlSession = sqlSession;
            }
            @Override
            public User getUserById(Long id) {
            return sqlSession.selectOne("com.example.dao.UserDao.getUserById", id);
            }
            // 其他方法的實現(xiàn)
            }

               這里使用 `SqlSession` 對象執(zhí)行 SQL 語句。

              7. 啟用 MyBatis:在 Spring Boot 應用程序的入口類上添加 `@MapperScan` 注解,指定需要掃描的 DAO 接口

              所在的包。

            @SpringBootApplication
            @MapperScan("com.example.dao")
            public class MyApp {
            public static void main(String[] args) {
            SpringApplication.run(MyApp.class, args);
            }
            }

               這樣,Spring Boot 將會自動掃描并注冊 DAO 接口的實現(xiàn)類。

              通過以上步驟,你就可以在 Spring Boot 中成功整合 MyBatis。MyBatis 是一個強大的持久化框架,結(jié)合 Spring Boot 使用可以方便地進行數(shù)據(jù)庫操作和數(shù)據(jù)訪問。

            tags: springboot
            聲明:本站稿件版權均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強師集結(jié),手把手帶你蛻變精英
            請您保持通訊暢通,專屬學習老師24小時內(nèi)將與您1V1溝通
            免費領取
            今日已有369人領取成功
            劉同學 138****2860 剛剛成功領取
            王同學 131****2015 剛剛成功領取
            張同學 133****4652 剛剛成功領取
            李同學 135****8607 剛剛成功領取
            楊同學 132****5667 剛剛成功領取
            岳同學 134****6652 剛剛成功領取
            梁同學 157****2950 剛剛成功領取
            劉同學 189****1015 剛剛成功領取
            張同學 155****4678 剛剛成功領取
            鄒同學 139****2907 剛剛成功領取
            董同學 138****2867 剛剛成功領取
            周同學 136****3602 剛剛成功領取
            相關推薦HOT