Mybatis环境搭建(IDEA+⾮maven)Mybatis环境搭建(IDEA+⾮maven)
结构层次
conf放配置⽂件
lib放置导⼊的jar包
src是写java代码
bean 实体层
dao 接⼝层
test 测试代码
步骤
1.分层
将层次建⽴好conf,lib,是IDEA中的Directory.src下的是按照Java的层次分的
2.导包
包的作⽤,hamcrest-core-1.3.jar 不清楚与junit4⼀同导⼊的,没有这个包可能会保错
junit4.13-beta-3.jar 单元测试的包
log4j.jar 不是很清楚,在控制台显⽰输出数据库结果,
mybatis-3.4 框架核⼼包
mysql-connector-java-5.1.12数据库连接
将这⼏个包导⼊到lib下后,**⼀定要add as library **否则没有⽤
3.准备配置⽂件
l这个⽂件不需要修改,作⽤:与log4j.jar包配合
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<configuration log4j="/log4j/">
<appender name="STDOUT"class="org.apache.log4j.ConsoleAppender">
<param name="Encoding"value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver"value="${jdbc.driver}"/>
<property name="url"value="${jdbc.url}"/>
<property name="username"value="${jdbc.username}"/>
<property name="password"value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 将我们写好的sql映射⽂件(l)⼀定要注册到全局配置⽂件(l)中 -->
<mappers>
<mapper resource="l"/>
<mapper class="com.ni.dao.StudentMapperAnnotation"></mapper>
</mappers>
</configuration>
<properties resource="jdbc.properties"></properties>
<    property name="driver" value="${jdbc.driver}" />
<property name="url"value="${jdbc.url}"/>
<property name="username"value="${jdbc.username}"/>
<property name="password"value="${jdbc.password}"/>
说明:value值避免有空格,否则会产⽣不到驱动的异常<br />与映射⽂件对应的部分第⼆个mapper可以先不管
<mappers>
<mapper resource="l"/>
<mapper class="com.ni.dao.StudentMapperAnnotation"></mapper>
</mappers>
3.jdbc.properties⽂件
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/aaa?useSSL=FALSE
jdbc.username=root
jdbc.password=root
说明:注意不要在后⾯有空格,否则会产⽣不到数据库源的异常 看第⼆段的写法在数据库名称后⾯有useSSL=FALSE 不确定是否需要,有的博客中是TRUE,⼤的影响应该没有,应该是⼀种避免验证,不写我的也没有报过异常
l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ni.dao.StudentMapper">
<!--
namespace:名称空间;指定为接⼝的全类名
id:唯⼀标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Student getStuById(Integer id);
-->
<select id="getStuById"resultType="com.ni.bean.Student">
select id,name,age  from stu where id = #{id}
</select>
<!-- public void addStu(Student stu);
public void updateStu(Student stu);
public void deleteStuById(Integer id);-->
<!--
useGeneratedKeys="true";使⽤⾃增主键获取主键值策略
keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性
-->
<insert id="addStu"parameterType="com.ni.bean.Student"useGeneratedKeys="true"keyProperty="id">
insert into stu(id,name,age)values (#{id},#{name},#{age})
</insert>
<update id="updateStu">
update stu set age = #{age},name = #{name} where id = #{id}
</update>
<delete id ="deleteStuById">
delete from stu where id = #{id}
</delete>
<!--public void  getStuByIdAndAge(Integer id,Integer age);-->
<!--  /*  select * from stu where id = #{id} and age = #{age}  *//*不到id,多个参数特殊处理*/
/*多个参数会封装成⼀个map key and value value 参数值*/-->
<!--标签内不能写注释....-->
<!--    select * from stu where id = #{param1} and age= #{param2}-->
<!--上⾯的⽅法不够清晰,最终:@param注解map中的key -->
<!---->
<select id="getStuByIdAndAge"resultType="com.ni.bean.Student">
select * from stu where id = #{id} and age= #{age}
</select>
<!-- public Student getStuByAge(Integer age);-->
<select id="getStuByAge"resultType="com.ni.bean.Student">
select * from stu where  age = #{age}
</select>
<!-- public Student getStuByMap(Map<String,Object> map);-->
<select id="getStuByMap"resultType="com.ni.bean.Student">
idea debug
select * from stu where age = #{age}
</select>
<!--public Student getStusByAge(List<Integer> age);-->
<select id="getStusByAge"resultType="com.ni.bean.Student">
select * from stu where age = #{age}
</select>
</mapper>
说明:上⾯的映射⽂件,增上改查的标签内不要写注释,本⼈在标签内写注释,⼀直被当做sql语句被编译⼏个注意点

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。