spring challenge02 mybatis尝试01

mybatis 基本配置,官方似乎更推荐xml配置

-----

---美丽的分割线----------------------------------------------------------------------

主要用到的文件

看工程结构

图中的mapper.xml

  ---dbsetting.xml

       ---mapper.java为关键

哈哈,命名不规范,哈哈哈哈

mapper.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="z.talent.mapper">
<select id="selectBlog" resultType="z.talent.anwser">
select * from ry where id = #{id}
</select>
</mapper>

-------------------------

dbsetting.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/erya"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="z/talent/mapper.xml"/>
</mappers>
</configuration>

-----------------------------------------

mapper.java

------------------

package z.talent;

public interface mapper {

}

----------------------------

流程

先配置数据库config(dbsetting.xml)

<dataSource type="POOLED">

有三种type 1 pooled 2 unpooled 3 jndi

     接着就是四个数据库属性值

     在声明个mapper的配置文件(mapper.xml)

紧接着说明mapper.xml

     <mapper namespace="z.talent.mapper">

作用域       z.talent.mapper就是mapper.java 文件 是个接口(interface)

     你可以在mapper里设置增删改查 也可以在 接口文件mapper里实现,反xml也是映射到mapper.java的,差不多,哈哈,我个小白的理解哈哈

     官方的demo 实在java mapper文件接口写的

      如下

public interface AutoConstructorMapper {
  @Select("SELECT * FROM subject WHERE id = #{id}")
  PrimitiveSubject getSubject(final int id);

  @Select("SELECT * FROM subject")
  List<PrimitiveSubject> getSubjects();

  @Select("SELECT * FROM subject")
  List<WrapperSubject> getWrapperSubjects();

  @Select("SELECT * FROM subject")
  List<AnnotatedSubject> getAnnotatedSubjects();

  @Select("SELECT * FROM subject")
  List<BadSubject> getBadSubjects();
}
View Code

xml应该易于维护哈哈

<select id="selectBlog" resultType="z.talent.anwser">
select * from ry where id = #{id}
</select>

更多查语句访问官网http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

配置完了开始使用

还有一件事  写个bean文件哦  set get 名与数据库字段要一致哦

public class Getdata {
	SqlSessionFactory sqlSessionFactory;
	public String getanwser() throws IOException {
		String resource = "z/talent/dbsettings.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		  anwser blog = (anwser) session.selectOne("z.talent.mapper.selectBlog", 101);
		  session.close();
		return blog.getAnwser();
	}
	
}

  似乎这么写 不好  

明天继续研究demo

sqlsessionfactory

然后获配置文件,然后条用语句

额,写的乱七八糟的

明天继续研究、

java功底还不够,还要继续研究;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

原文地址:https://www.cnblogs.com/zhangtalent/p/8299853.html