当前位置: 首页 > 工具软件 > ANMS-Codes > 使用案例 >

Mybatis codes

景宏富
2023-12-01

ibatis-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>

<properties resource="db.properties"></properties>

<settings>

<setting name="logImpl" value="STDOUT_LOGGING"/>

</settings>

<typeAliases>

<typeAlias alias="Blog" type="net.blue.Blog"/>

<typeAlias alias="BlogB" type="net.blue.BlogBlue"/>

</typeAliases>

<environments default="development">

<environment id="development">

<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>

<mappers>

<mapper resource="net/blue/BlogMapper.xml"/>

<mapper resource="net/blue/BlogMapperOld.xml"/>

<mapper class="net.blue.BlogMapperAn"/>

</mappers>

</configuration>

db.properties

driver=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/blue

username=root

password=root

SQL

create table blog (id int, author varchar(50), title varchar(50), content varchar(100));

insert into blog values(101, 'John', 'Sunday',' Today is a happy day');

insert into blog values(102, 'Rose', 'Monday',' I am m busy');

insert into blog values(103, 'George', 'Tuesday',' I am working hard');

BlogMapper.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="net.blue.BlogMapper">

<resultMap id = "blue" type = "BlogB">

<result property = "mid" column = "id"/>

<result property = "manthor" column = "anthor"/>

<result property = "mtitle" column = "title"/>

<result property = "mcontent" column="content"/>

</resultMap>

<select id="selectBlog" parameterType = "int" resultType="Blog">

select * from Blog where id = #{id}

</select>

<select id="selectBlogBlue" parameterType = "int" resultMap="blue">

select * from Blog where id = #{id}

</select>

<select id="selectBlogs" resultType="Blog">

select * from Blog

</select>

<select id="selectBlogMap" resultType="map">

select id, author, title, content

from Blog

</select>

<select id="selectBlogHashMap" resultType="hashmap">

select id, author, title, content

from Blog

</select>

<insert id="insertBlog">

insert into Blog (id,author,title,content)

values (#{id},#{author},#{title},#{content})

</insert>

</mapper>

BlogMapper

package net.blue;

import java.util.List;

public interface BlogMapper {

public Blog selectBlog(int i) ;

public BlogBlue selectBlogBlue(int i) ;

public List<Blog> selectBlogs();

public List selectBlogMap();

public List selectBlogHashMap();

public void insertBlog(int id, String author, String title, String content);

public void insertBlog(Blog log);

}

package net.blue;

import java.util.Map;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

public interface BlogMapperAn {
    @Select("SELECT * FROM blog WHERE id = #{id}")
    public Blog selectBlog(int i) ;
    
    @SelectProvider(type=BlogSQLProvider.class, method="selectBlogSql")
    public Blog selectBlogDy(int i) ;
    
    @SelectProvider(type=BlogSQLProvider.class, method="insertBlogSql")
    public void insertBlog(Blog blog);
    
    @SelectProvider(type=BlogSQLProvider.class, method="updateBlogSql")
    public void updateBlog(Blog log);
    
    @SelectProvider(type=BlogSQLProvider.class, method="updateBlogSql")
    public void updateBlogPara(Map<String, Object> hashMap);

    
    public class BlogSQLProvider {
        public String updateBlogSql() {        
            return new SQL()
                .UPDATE("Blog")
                .SET("author = #{author}")
                .WHERE("ID = #{id}")
                .toString();
        }
        public String selectBlogSql() {
              return new SQL()
                .SELECT("id,author, title, content")
                .FROM("Blog")
                .WHERE("id = #{id}")
                .toString();
        }
        public String insertBlogSql() {
              return new SQL()
                .INSERT_INTO("Blog")
                .INTO_COLUMNS("id,author","title")
                .VALUES("content","#{id},#{author},#{title},#{content}")
                .toString();
        }
    }

}

 BlogMapperOld.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="net.blue.BlogMapperOld">

<select id="selectBlog" resultType="net.blue.Blog">

select * from Blog where id = #{id}

</select>

</mapper>

public static void mainBak(String[] args) {

String resource = "net/blue/mybatis-config.xml";

InputStream inputStream;

try {

inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sqlSessionFactory.openSession();

Blog blogOld = (Blog) session.selectOne("net.blue.BlogMapperOld.selectBlog", 101);

System.out.println(blogOld.getContent());

BlogMapper mapper = session.getMapper(BlogMapper.class);

Blog blog = mapper.selectBlog(102);

System.out.println(blog.getContent());

System.out.println("--------------------------------");

List<Blog> map=mapper.selectBlogs();

for(Blog log: map) {

System.out.println(log.getContent());

}

System.out.println("--------------Map------------------");

List<Map> list=mapper.selectBlogMap();

for(Map mapItem:list) {

System.out.println();

for(Object key: mapItem.keySet().toArray()) {

System.out.print(" "+key+"="+mapItem.get(key));

}

}

System.out.println();

System.out.println("---------------***HashMap***-----------------");

List<HashMap> listMap=mapper.selectBlogHashMap();

for(Map mapItem:listMap) {

System.out.println();

for(Object key: mapItem.keySet().toArray()) {

System.out.print(" "+key+"="+mapItem.get(key));

}

}

System.out.println();

BlogMapperAn mapperAn = session.getMapper(BlogMapperAn.class);

Blog blogAn = mapper.selectBlog(103);

System.out.println("Annotation: "+blogAn.getContent());

} catch (Exception e) {

e.printStackTrace();

}

}

package net.blue;

import org.apache.ibatis.annotations.Select;

public interface BlogMapperOld {

public Blog selectBlog(int i) ;

}

public static void main(String[] args) {

String resource = "net/blue/mybatis-config.xml";

InputStream inputStream;

try {

inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSession session = sqlSessionFactory.openSession(true);

BlogMapperAn mapper = session.getMapper(BlogMapperAn.class);

Blog blog=new Blog(105, "Jeniffer", "Sky", "The sky is clear and blue!");

mapper.insertBlog(blog);

HashMap<String, Object> paraMap=new HashMap();

paraMap.put("id", 104);

paraMap.put("author", "happydog");

mapper.updateBlogPara(paraMap);

session.close();

//session.commit();

} catch (Exception e) {

e.printStackTrace();

}

}

 类似资料:

相关阅读

相关文章

相关问答