标签:大小 root cat 划线 注册 图片 pack cep result
db.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://192.168.8.136:3306/mybatis username=root password=root
mybatis-config.xml
<!-- 外部 properties 配置文件 --> <properties resource="db.properties"/> <!--配置数据源--> <environments default="development-mysql"> <environment id="development-mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments>
mybatis-config.xml
<settings> <!-- 数据库字段下划线转 Bean 字段的驼峰命名 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 控制台打印SQL --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings>
<!-- typeAliases:别名处理器:可以为 JavaBean 类型起别名,别名不区分大小写 --> <typeAliases> <!-- typeAlias:为某个java类型起别名,type:指定要起别名的类型全类名;默认别名就是类名小写,alias:指定新的别名 --> <!--<typeAlias type="com.bean.MyUser" alias="myUser"/>--> <!-- package:为某个包下的所有类批量起别名,name:指定包名,为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写) --> <package name="com.bean"/> <!-- 批量起别名的情况下,可以在 Bean 上使用 @Alias 注解为某个类指定新的别名 --> </typeAliases>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace 对应接口文件的全路径 --> <mapper namespace="com.dao.MyUserMapper"> <!-- id 与接口文件方法名对应,resultType 设置别名后返回类型可写别名 --> <!--<select id="selectMyUser" resultType="com.bean.MyUser">--> <select id="selectMyUser" resultType="myUser"> select * from myuser where id = #{id} </select> </mapper>
<!-- environments:环境配置,default 指定使用某种环境 environment:配置具体环境信息;有两个标签;id代表当前环境唯一标识 transactionManager:事务管理器 type:事务管理器的类型 JDBC(JdbcTransactionFactory) | MANAGED(ManagedTransactionFactory) 自定义事务管理器:实现 TransactionFactory 接口,type 为全类名 dataSource:数据源 type:数据源类型 UNPOOLED(UnpooledDataSourceFactory) | POOLED(PooledDataSourceFactory) | JNDI(JndiDataSourceFactory) 自定义数据源:实现 DataSourceFactory 接口,type 为全类名 --> <environments default="development-dev"> <environment id="development-dev"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="development-sit"> <transactionManager type="MANAGED"/> <dataSource type="UNPOOLED"> <property name="driver" value="${sit.driver}"/> <property name="url" value="${sit.url}"/> <property name="username" value="${sit.username}"/> <property name="password" value="${sit.password}"/> </dataSource> </environment> </environments>
typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class); typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class); typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class); typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class); typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class); typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class); typeAliasRegistry.registerAlias("FIFO", FifoCache.class); typeAliasRegistry.registerAlias("LRU", LruCache.class); typeAliasRegistry.registerAlias("SOFT", SoftCache.class); typeAliasRegistry.registerAlias("WEAK", WeakCache.class); typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class); typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class); typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class); typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class); typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class); typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class); typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class); typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class); typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class); typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class); typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class); typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class); languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class); languageRegistry.register(RawLanguageDriver.class);
<!-- environments:环境配置,default 指定使用某种环境 environment:配置具体环境信息;有两个标签;id代表当前环境唯一标识 transactionManager:事务管理器 type:事务管理器的类型 JDBC(JdbcTransactionFactory) | MANAGED(ManagedTransactionFactory) 自定义事务管理器:实现 TransactionFactory 接口,type 为全类名 dataSource:数据源 type:数据源类型 UNPOOLED(UnpooledDataSourceFactory) | POOLED(PooledDataSourceFactory) | JNDI(JndiDataSourceFactory) 自定义数据源:实现 DataSourceFactory 接口,type 为全类名 --> <environments default="development-mysql"> <environment id="development-mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="development-oracle"> <transactionManager type="MANAGED"/> <dataSource type="UNPOOLED"> <property name="driver" value="${oracle.driver}"/> <property name="url" value="${oracle.url}"/> <property name="username" value="${oracle.username}"/> <property name="password" value="${oracle.password}"/> </dataSource> </environment> </environments> <!-- databaseIdProvider:支持多数据库厂商 type="DB_VENDOR":VendorDatabaseIdProvider 作用是得到数据库厂商的标识(JDBC getDatabaseProductName()),mybatis 就能根据数据库厂商标识来执行不同的 SQL --> <databaseIdProvider type="DB_VENDOR"> <!-- 为不同的数据库厂商起别名 --> <property name="MySQL" value="mysql"/> <property name="Oracle" value="oracle"/> <property name="SQL Server" value="sqlserver"/> </databaseIdProvider>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace 对应接口文件的全路径 --> <mapper namespace="com.dao.MyUserMapper"> <!--找不到对应数据库 SQL 语句时会执行默认--> <select id="selectMyUser" resultType="myUser"> select * from myuser where id = #{id} </select> <!-- databaseId 对应数据库别名 --> <select id="selectMyUser" resultType="myUser" databaseId="mysql"> select * from myuser where id = #{id} </select> <select id="selectMyUser" resultType="myUser" databaseId="oracle"> select * from myuser where id = #{id} </select> </mapper>
<mappers> <!-- 引用类路径下的 SQL 映射文件 --> <mapper resource="com/dao/MyUserMapper.xml"/> </mappers>
<mappers> <!-- 引用网路路径或者磁盘路径下的sql映射文件 --> <mapper url="file:///var/mappers/AuthorMapper.xml"/> </mappers>
<mappers> <!-- 引用 Class 接口注册,没有 SQL 映射文件,所有的 SQL 都是利用注解写在接口上 --> <mapper class="com.dao.MyUserMapperAnnotation"/> </mappers>
MyUserMapperAnnotation
import com.bean.MyUser; import org.apache.ibatis.annotations.Select; public interface MyUserMapperAnnotation { @Select("select * from myuser where id = #{id}") public MyUser selectMyUser(Integer id); }
测试
import com.bean.MyUser; import com.dao.MyUserMapperAnnotation; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class Main { public static void main(String[] args) { SqlSession session = null; try { String resource = "mybatis-config2.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); session = sqlSessionFactory.openSession(); MyUserMapperAnnotation mapper = session.getMapper(MyUserMapperAnnotation.class); MyUser myUser = (MyUser) mapper.selectMyUser(1); System.out.println(myUser); } catch (IOException e) { e.printStackTrace(); } finally { if (session != null) { session.close(); } } } }
<mappers> <!-- 批量注册,映射文件名必须和接口同名,且放在与接口同一层级目录下 --> <package name="com.dao"/> </mappers>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 外部 properties 配置文件 --> <properties resource="db.properties"/> <!-- settings包含很多重要的设置项,setting:用来设置每一个设置项。name:设置项名,value:设置项取值 --> <settings> <!-- 数据库字段下划线转 Bean 字段的驼峰命名 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 控制台打印SQL --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- typeAliases:别名处理器:可以为 JavaBean 类型起别名,别名不区分大小写 --> <typeAliases> <!-- typeAlias:为某个java类型起别名,type:指定要起别名的类型全类名;默认别名就是类名小写,alias:指定新的别名 --> <!--<typeAlias type="com.bean.MyUser" alias="myUser"/>--> <!-- package:为某个包下的所有类批量起别名,name:指定包名,为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写) --> <package name="com.bean"/> <!-- 批量起别名的情况下,可以在 Bean 上使用 @Alias 注解为某个类指定新的别名 --> </typeAliases> <!-- environments:环境配置,default 指定使用某种环境 environment:配置具体环境信息;有两个标签;id代表当前环境唯一标识 transactionManager:事务管理器 type:事务管理器的类型 JDBC(JdbcTransactionFactory) | MANAGED(ManagedTransactionFactory) 自定义事务管理器:实现 TransactionFactory 接口,type 为全类名 dataSource:数据源 type:数据源类型 UNPOOLED(UnpooledDataSourceFactory) | POOLED(PooledDataSourceFactory) | JNDI(JndiDataSourceFactory) 自定义数据源:实现 DataSourceFactory 接口,type 为全类名 --> <environments default="development-mysql"> <environment id="development-mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="development-oracle"> <transactionManager type="MANAGED"/> <dataSource type="UNPOOLED"> <property name="driver" value="${oracle.driver}"/> <property name="url" value="${oracle.url}"/> <property name="username" value="${oracle.username}"/> <property name="password" value="${oracle.password}"/> </dataSource> </environment> </environments> <!-- databaseIdProvider:支持多数据库厂商 type="DB_VENDOR":VendorDatabaseIdProvider 作用是得到数据库厂商的标识(JDBC getDatabaseProductName()),mybatis 就能根据数据库厂商标识来执行不同的 SQL --> <databaseIdProvider type="DB_VENDOR"> <!-- 为不同的数据库厂商起别名 --> <property name="MySQL" value="mysql"/> <property name="Oracle" value="oracle"/> <property name="SQL Server" value="sqlserver"/> </databaseIdProvider> <!-- mappers:将 SQL 映射文件注册到全局配置中 --> <mappers> <!-- 引用类路径下的 SQL 映射文件 --> <!--<mapper resource="com/dao/MyUserMapper.xml"/>--> <!-- 引用网路路径或者磁盘路径下的sql映射文件 --> <!--<mapper url="file:///var/mappers/AuthorMapper.xml"/>--> <!-- 引用 Class 接口注册,没有 SQL 映射文件,所有的 SQL 都是利用注解写在接口上 --> <!--<mapper class="com.dao.MyUserMapperAnnotation"/>--> <!-- 批量注册,映射文件名必须和接口同名,且放在与接口同一层级目录下 --> <package name="com.dao"/> </mappers> </configuration>
标签:大小 root cat 划线 注册 图片 pack cep result
原文地址:https://www.cnblogs.com/jhxxb/p/10475308.html