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&useSSL=false&serverTimezone=GMT%2B8&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;