标签:bean mat code artifact cts ack dao层 model hone
1.resources下添加spring
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!-- 自动扫描com.zll.springboot.demo包,包中用了@controller注解的类是控制器 --> <context:component-scan base-package="com.zll.springboot.demo" /> <!--引入数据库配置文件--> <import resource="spring-db.xml"/> </beans>
1 #数据库连接:地址、用户名、密码 2 #数据库地址后加上一些编码格式,使得不出现乱码 3 spring.datasource.url=jdbc:mysql://url:port/database_name?useUnicode=true&characterEncoding=utf8&useOldAliasMetadataBehavior=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true 4 spring.datasource.username=user_name 5 spring.datasource.password=user_password 6 #driver 7 spring.datasource.driverClassName=com.mysql.jdbc.Driver 8 #映射文件所在地址,表示resources/mapper/*.xml 9 mybatis.mapperLocations=classpath:mapper/*.xml 10 server.port=8011 11 server.session.timeout=10 12 server.tomcat.uri-encoding=UTF-8
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 5 http://www.springframework.org/schema/context 6 http://www.springframework.org/schema/context/spring-context-3.2.xsd 7 http://www.springframework.org/schema/tx 8 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 9 10 <!-- 加载配置JDBC文件 --> 11 <context:property-placeholder location="classpath:application.properties" /> 12 <!-- 数据源 ${jdbc.url}这个要和上面application.properties中的一样 --> 13 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 14 <property name="driverClassName"> 15 <value>${jdbc.driverClassName}</value> 16 </property> 17 <property name="url"> 18 <value>${jdbc.url}</value> 19 </property> 20 <property name="username"> 21 <value>${jdbc.username}</value> 22 </property> 23 <property name="password"> 24 <value>${jdbc.password}</value> 25 </property> 26 </bean> 27 28 <!-- 开启注解配置 即Autowried --> 29 <!-- <context:annotation-config/> --> 30 <!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 --> 31 <context:component-scan base-package="cn.zll.springboottest.dao" /> 32 33 <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory --> 34 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 35 <property name="dataSource" ref="dataSource" /> 36 <!-- mybatis配置文件路径 --> 37 <property name="configLocation" value="" /> 38 <!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 --> 39 <property name="mapperLocations" value="classpath:mapper/*.xml" /> 40 </bean> 41 42 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 43 <constructor-arg index="0"> 44 <ref bean="sqlSessionFactory" /> 45 </constructor-arg> 46 </bean> 47 48 <!--动态代理实现 不用写dao的实现 --> 49 <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 50 <!-- 这里的basePackage 指定了dao层接口路劲,这里的dao接口不用自己实现 --> 51 <property name="basePackage" value="com.wacai.qa.springboot.demo" /> 52 <!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 --> 53 <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> 54 <!--直接制定了sqlsessionTemplate名称,这个和上面的其实是一样的 --> 55 <property name="sqlSessionTemplateBeanName" value="sqlSession" /> 56 </bean> 57 58 <!--事务管理器 --> 59 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 60 <property name="dataSource" ref="dataSource" /> 61 </bean> 62 <!-- 使用全注释事务 --> 63 <tx:annotation-driven transaction-manager="transactionManager" /> 64 </beans>
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-jdbc</artifactId> 4 <version>5.0.1.RELEASE</version> 5 </dependency> 6 <dependency> 7 <groupId>org.mybatis</groupId> 8 <artifactId>mybatis</artifactId> 9 <version>3.4.0</version> 10 </dependency> 11 <dependency> 12 <groupId>org.mybatis</groupId> 13 <artifactId>mybatis-spring</artifactId> 14 <version>1.3.0</version> 15 </dependency>
1 <dependency> 2 <groupId>mysql</groupId> 3 <artifactId>mysql-connector-java</artifactId> 4 <version>6.0.4</version> 5 </dependency>
2.开始写sql啦
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!--命名空间dao--> 4 <mapper namespace="com.springboot.demo.dao.UserDao"> 5 <!--sql结果与entity的映射--> 6 <resultMap id="userResult" type="com.springboot.demo.entity.User"> 7 <id column="id" property="id" jdbcType="BIGINT"/> 8 <result column="nick_name" property="nickName" jdbcType="VARCHAR"/> 9 <result column="email" property="email" jdbcType="VARCHAR"/> 10 <result column="phone_number" property="phoneNumber" jdbcType="VARCHAR"/> 11 </resultMap> 12 <!--数据库基本字段--> 13 <sql id="Base_Column_List"> 14 id, nick_name, email, phone_number 15 </sql> 16 <!--写sql语句,不同的方法用不同标签,如insert/select/delete等--> 17 <insert id="insert"> 18 INSERT INTO user (nick_name, email, phone_number) VALUES ( #{nickName},#{email}, #{phoneNumber}) 19 </insert> 20 <!--select会返回结果--> 21 <select id="query" resultMap="userResult"> 22 SELECT 23 <include refid="Base_Column_List"/> 24 from user where id=#{id} 25 </select> 26 </mapper>
3.反向工程,从数据库来创建dao和entity对象
1 <plugin> 2 <groupId>org.mybatis.generator</groupId> 3 <artifactId>mybatis-generator-maven-plugin</artifactId> 4 <version>1.3.5</version> 5 <configuration> 6 <verbose>true</verbose> 7 <overwrite>true</overwrite> 8 </configuration> 9 </plugin>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 <!--mysql 连接数据库jar 这里选择自己本地位置,可以在external Libraries中查找位置和版本--> 7 <classPathEntry location="/Users/zll/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar" /> 8 <context id="testTables" targetRuntime="MyBatis3"> 9 <commentGenerator> 10 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 11 <property name="suppressAllComments" value="true" /> 12 </commentGenerator> 13 <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> 14 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 15 connectionURL="jdbc:mysql://url:port/databasename" userId="user_name" 16 password="user_password"> 17 </jdbcConnection> 18 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal --> 19 <javaTypeResolver> 20 <property name="forceBigDecimals" value="false" /> 21 </javaTypeResolver> 22 23 <!--下面是几个要生成的文件的位置配置--> 24 <!--生成entity,位置targetProject/targetPackage --> 25 <javaModelGenerator targetPackage="com.zll.springboot.demo.entity" targetProject="src/main/java"> 26 <!-- enableSubPackages:是否让schema作为包的后缀 --> 27 <property name="enableSubPackages" value="false" /> 28 <!-- 从数据库返回的值被清理前后的空格 --> 29 <property name="trimStrings" value="true" /> 30 </javaModelGenerator> 31 <!-- targetProject:mapper映射文件生成的位置。如果maven工程只是单独的一个工程,targetProject="src/main/java"。若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:targetProject="ecps-manager-mapper",下同--> 32 <sqlMapGenerator targetPackage="com.zll.springboot.demo.persistence" 33 targetProject="src/main/java"> 34 <!-- enableSubPackages:是否让schema作为包的后缀 --> 35 <property name="enableSubPackages" value="false" /> 36 </sqlMapGenerator> 37 <!-- targetPackage:mapper接口生成的位置 --> 38 <javaClientGenerator type="XMLMAPPER" targetPackage="com.zll.springboot.demo.persistence" targetProject="src/main/java"> 39 <!-- enableSubPackages:是否让schema作为包的后缀 --> 40 <property name="enableSubPackages" value="false" /> 41 </javaClientGenerator> 42 <!-- 指定数据库表 这里写要转换的表名,比如下面写了baidu_region,那就会将这个表转换过来--> 43 <table schema="" tableName="baidu_region"></table> 44 </context> 45 </generatorConfiguration>
标签:bean mat code artifact cts ack dao层 model hone
原文地址:http://www.cnblogs.com/coolqiyu/p/7874361.html