1、mybatis+spring
-
配置 applicationContext.xml 数据库使用c3p0 sqlSessionFactory被集成为一个bean
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 引入数据库配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 初始连接池大小 --> <property name="initialPoolSize" value="2"/> <!-- 连接池中连接最小个数 --> <property name="minPoolSize" value="1"/> <property name="maxPoolSize" value="5"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> <!--扫描mapper 并称为bean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源 保证了数据库的四大特性--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 每次调用这些方法都会启动 事务管理 每个在config里配置的方法都要以以下方式开头--> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切点 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.buyerApp.*.*.*.service.*(..))"/> </aop:config> <!--每次调用service层的数据库语句都会收到上面各个方法的通知,若代码错误 数据库就会回滚 --> <bean id="user" class="com.pojo.User"></bean> </beans>
-
配置数据库连接信息 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/buyerapp?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456 jdbc.initialPoolSize=5 jdbc.minPoolSize=1 jdbc.maxPoolSize=10
-
配置逆向工程文件 generator.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- suppressAllComments属性值: true:自动生成实体类、SQL映射文件时没有注释 false:自动生成实体类、SQL映射文件,并附有注释 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/buyerapp?characterEncoding=UTF-8" userId="root" password="123456"> </jdbcConnection> <!-- forceBigDecimals属性值: true:把数据表中的DECIMAL和NUMERIC类型, 解析为JAVA代码中的java.math.BigDecimal类型 false(默认):把数据表中的DECIMAL和NUMERIC类型, 解析为解析为JAVA代码中的Integer类型 --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject属性值:实体类pojo的生成位置 targetPackage属性值:实体类所在包的路径 --> <javaModelGenerator targetPackage="com.pojo" targetProject="Spring-Mybatis/src"> <!-- trimStrings属性值: true:对数据库的查询结果进行trim操作 false(默认):不进行trim操作 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject属性值:SQL映射文件的生成位置(写数据库语句类) targetPackage属性值:SQL映射文件所在包的路径(xml) --> <sqlMapGenerator targetPackage="com.mapper" targetProject="Spring-Mybatis/src"> </sqlMapGenerator> <!-- 生成动态代理的接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="Spring-Mybatis/src"> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="admin"> <property name="useActualColumnNames" value="false"/> </table> </context> </generatorConfiguration>
-
测试
public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = (UserMapper) ac.getBean("userMapper"); User user = (User)ac.getBean("user"); user.setUserPhone("123"); user.setUserPwd("321"); int result = userMapper.insert(user); if (result>0) { System.out.println("添加成功"); }else { System.out.println("添加失败"); } }