首页 > 代码库 > MyBatis 模板
MyBatis 模板
mybatis-config.xml:
<?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> <!-- property from external resources --> <properties resource="config/applications.properties"/> <!-- settings --> <settings> <setting name="cacheEnabled" value="true"/> <setting name="logImpl" value="COMMONS_LOGGING"/> <!-- <setting name="lazyLoadingEnabled" value="http://www.mamicode.com/true" /> <setting name="multipleResultSetsEnabled" value="http://www.mamicode.com/true" /> <setting name="useColumnLabel" value="http://www.mamicode.com/true" /> <setting name="useGeneratedKeys" value="http://www.mamicode.com/false" /> <setting name="autoMappingBehavior" value="http://www.mamicode.com/PARTIAL" /> <setting name="defaultExecutorType" value="http://www.mamicode.com/SIMPLE" /> <setting name="defaultStatementTimeout" value="http://www.mamicode.com/25000" /> <setting name="safeRowBoundsEnabled" value="http://www.mamicode.com/false" /> <setting name="mapUnderscoreToCamelCase" value="http://www.mamicode.com/false" /> <setting name="localCacheScope" value="http://www.mamicode.com/SESSION" /> <setting name="jdbcTypeForNull" value="http://www.mamicode.com/OTHER" /> <setting name="lazyLoadTriggerMethods" value="http://www.mamicode.com/equals,clone,hashCode ,toString" /> --> </settings> <!-- type aliases(full class name -> simple class name) --> <typeAliases> <!-- <typeAlias alias="Student" type="com.mybatis3.domain.Student" /> --> <package name="per.piers.onlineJudge.model"/> </typeAliases> <!-- type handlers --> <typeHandlers> <typeHandler handler="per.piers.onlineJudge.handler.SexTypeHandler" javaType="per.piers.onlineJudge.model.Sex" jdbcType="BOOLEAN"/> </typeHandlers> <!-- environment --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> <environment id="production"> <transactionManager type="MANAGED"/> <dataSource type="JNDI"> <property name="data_source" value="java:comp/jdbc/mybatis"/> </dataSource> </environment> </environments> <!-- mappers location --> <mappers> <!-- <mapper url="file:///D:/mybatisdemo/app/mappers/TutorMapper.xml" /> <mapper class="com.mybatis3.mappers.TutorMapper" /> --> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
applications.properties:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/online_judge?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=true jdbc.username=root jdbc.password=woaimysql-135
context.xml(JNDI配置数据源):
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="mybatis" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/onlineJudge" username="root" password="woaimysql-135" maxTotal="20" maxIdle="10" maxWaitMillis="10000" /> </Context>
UserMapper.xml:
<?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"> <mapper namespace="per.piers.onlineJudge.mapper.UserMapper"> <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO online_judge.users (email, password, name, sex, role, enabled) VALUES (#{email}, #{password}, #{name}, #{sex}, #{role}, #{enabled}) </insert> <update id="updateUser" parameterType="User"> UPDATE online_judge.users SET email = #{email}, password = #{password}, name = #{name}, sex = #{sex}, role = #{role}, enabled = #{enabled} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM online_judge.users WHERE id = #{id} </delete> <select id="selectUserByEmail" parameterType="String" resultMap="userResult"> SELECT * FROM online_judge.users WHERE email = #{email} </select> <resultMap id="userResult" type="User"> <id column="id" property="id"/> <result column="email" property="email"/> <result column="password" property="password"/> <result column="name" property="name"/> <result column="sex" property="sex"/> <result column="enabled" property="enabled"/> <result column="role" property="role"/> </resultMap> </mapper>
MyBatis 模板
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。