集成mybatis(一)

Spring MVC框架集成mybatis

1.配置Mybatis-Spring项目需要这么几步:

·配置数据源

·配置SqlSessionFactory

·配置Mapper

·事务管理

·可选择的配置有SqlSessionTemplate

配置数据源:

xml方式:

<!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/role?characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8&amp;allowPublicKeyRetrieval=true"/>
        <property name="username" value="root" />
        <property name="password" value="123456abc" />
        <property name="maxActive" value="255" />
        <property name="maxIdle" value="5" />
        <property name="maxWait" value="10000" />
    </bean>

config类配置:

/**
     * 配置数据库
     * @return 数据库连接池
     */
    @Bean(name="dataSource")
    public DataSource initDataSource() {
        if(dataSource!=null) {
            return dataSource;
        }
        Properties props=new Properties();
        props.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
        props.setProperty("url","jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true");
        props.setProperty("userName","root");
        props.setProperty("password","123456abc");
        props.setProperty("maxActive","20");
        props.setProperty("maxIdle","20");
        props.setProperty("maxWait","30000");
        try {
            dataSource=BasicDataSourceFactory.createDataSource(props);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return dataSource;
posted @ 2020-03-12 19:40  马中赤兔  阅读(127)  评论(0编辑  收藏  举报