欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

spring-mybatis獲取mapper方式匯總

項(xiàng)目背景:

pojo下面有一個(gè)user實(shí)體類

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

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

<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>

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

實(shí)現(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測(cè)試:

@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的注冊(cè):

<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>

測(cè)試:

?@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);
? ? ? ? }
? ? }

在使用這個(gè)MapperFactoryBean方式的時(shí)候,通過(guò)app有兩種方式獲取bean,一種是id然后強(qiáng)轉(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映射文件(那個(gè)xml寫的是真的無(wú)語(yǔ)。。。)。很惡心的一個(gè)事實(shí)是源碼中并沒有對(duì)于這個(gè)注解的詳細(xì)解釋

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

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

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

因?yàn)槲覀儧]有顯式標(biāo)注這是一個(gè) Bean,IDEA 認(rèn)為運(yùn)行的時(shí)候會(huì)找不到實(shí)例注入,所以提示我們錯(cuò)誤。

如下圖,會(huì)有紅色波浪線。

盡管這個(gè)錯(cuò)誤提示并不影響運(yùn)行,但是看起來(lái)很不舒服,所以我們可以在對(duì)應(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 注解,可以在啟動(dòng)類上添加該注解,自動(dòng)掃描包路徑下的所有接口。

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

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

但是感覺有些麻煩。

總結(jié)

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

相關(guān)文章

最新評(píng)論