亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring-mybatis獲取mapper的四種方式匯總

 更新時間:2023年03月07日 14:32:00   作者:不過普通話一乙不改名  
這篇文章主要介紹了spring-mybatis獲取mapper的四種方式匯總,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

spring-mybatis獲取mapper方式匯總

項目背景:

pojo下面有一個user實體類

Dao包下面寫了usermapper.xml 和usermapper.interface,其中只有一個方法查詢數(shù)據(jù)庫中所有的 用戶。

1.用實現(xiàn)類獲取這個用戶

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
	
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

	<!--注冊實現(xiàn)類usermapperimpl-->
    <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

實現(xiàn)類usermapperImpl:

public class UserMapperImpl implements UserMapper {
    private SqlSessionTemplate sqlSession;

    public List<User> selectAllUser() {
        return sqlSession.getMapper(UserMapper.class).selectAllUser();
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
}

test測試:

@Test
    public void test2(){
        ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapperImpl userMapper = app.getBean(UserMapperImpl.class);
        List<User> users = userMapper.selectAllUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

2.SqlSessionDaoSupport獲取

public class UserMapperImpl1 extends SqlSessionDaoSupport implements UserMapper {
? ? public List<User> selectAllUser() {
? ? ? ? return getSqlSession().getMapper(UserMapper.class).selectAllUser();
? ? }
}

bean的注冊:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/>
? ? </bean>

<bean id="userimpl1" class="com.kuang.mapper.UserMapperImpl1">
? ? ? ? <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
? ? </bean>

3.MapperFactoryBean

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
? ? ? ? <property name="username" value="root"/>
? ? ? ? <property name="password" value="root"/>
? ? </bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/>
? ? </bean>
? ??
<bean id="userimpl" class="org.mybatis.spring.mapper.MapperFactoryBean">
? ? ? ? <property name="mapperInterface" value="com.kuang.mapper.UserMapper"/>
? ? ? ? <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
? ? </bean>

測試:

?@Test
? ? public void test3(){
? ? ? ? ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
? ? ? ? UserMapper userMapper = app.getBean(UserMapper.class);
??? ??? ?// UserMapper userMapper = app.getBean("userimpl");
? ? ? ? List<User> users = userMapper.selectAllUser();
? ? ? ? for (User user : users) {
? ? ? ? ? ? System.out.println(user);
? ? ? ? }
? ? }

在使用這個MapperFactoryBean方式的時候,通過app有兩種方式獲取bean,一種是id然后強轉(zhuǎn),另一種是接口的類型class。

4.MapperScannerConfigurer

xml配置

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
? ? ? ? <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
? ? ? ? <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
? ? ? ? <property name="username" value="root"/>
? ? ? ? <property name="password" value="root"/>
? ? </bean>

? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml"/>
? ? </bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? <property name="basePackage" value="com.kuang.mapper"/>
? ? ? ? <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
? ? </bean>

test:

@Test
? ? public void test4(){
? ? ? ? ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
? ? ? ? UserMapper bean = app.getBean(UserMapper.class);
? ? ? ? for (User user : bean.selectAllUser()) {
? ? ? ? ? ? System.out.println(user);
? ? ? ? }
? ? }

mybatis的mapper注解

從mybatis3.4.0開始加入了@Mapper注解,目的就是為了不再寫mapper映射文件(那個xml寫的是真的無語。。。)。很惡心的一個事實是源碼中并沒有對于這個注解的詳細解釋

在 Spring 程序中,Mybatis 需要找到對應(yīng)的 mapper,在編譯的時候動態(tài)生成代理類,實現(xiàn)數(shù)據(jù)庫查詢功能,所以我們需要在接口上添加 @Mapper 注解。

@Mapper
public interface UserDao {
?? ?...
}

但是,僅僅使用@Mapper注解,我們會發(fā)現(xiàn),在其他變量中依賴注入,IDEA 會提示錯誤,但是不影響運行(親測~)。

因為我們沒有顯式標注這是一個 Bean,IDEA 認為運行的時候會找不到實例注入,所以提示我們錯誤。

如下圖,會有紅色波浪線。

盡管這個錯誤提示并不影響運行,但是看起來很不舒服,所以我們可以在對應(yīng)的接口上添加 bean 的聲明,如下:

@Repository // 也可以使用@Component,效果都是一樣的,只是為了聲明為bean
@Mapper
public interface UserDao {
	
	@Insert("insert into user(account, password, user_name) " +
            "values(#{user.account}, #{user.password}, #{user.name})")
    int insertUser(@Param("user") User user) throws RuntimeException;
}

基于注解的開發(fā)也有其他手段幫助 Mybatis 找到 mapper,那就是 @MapperScan 注解,可以在啟動類上添加該注解,自動掃描包路徑下的所有接口。

@SpringBootApplication
@MapperScan("com.scut.thunderlearn.dao")
public class UserEurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserEurekaClientApplication.class, args);
    }
}

但是感覺有些麻煩。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論