码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis入门

时间:2018-11-28 18:53:16      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:new   pack   parameter   直接   update   sele   into   tor   build   

使用的gradle来管理java代码,首先引入mybatis的依赖,还有mysql数据库连接的依赖
compile group: ‘mysql‘, name: ‘mysql-connector-java‘, version: ‘8.0.13‘
compile group: ‘org.mybatis‘, name: ‘mybatis‘, version: ‘3.4.6‘

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>
<!--加载jdbc.properties文件,下面的DataSource调用 -->
<properties resource="jdbc.properties"/>
<typeAliases>
    <!--取别名,在mapper配置文件中,parameterType就可以直接使用别名,而不写类路径了-->
    <typeAlias type="com.site.domain.User" alias="user"/>
</typeAliases>
<!--通过XML构建SQLSessionFactory:数据库环境 -->
<environments default="development">
    <environment id="development">
        <!--事务管理-->
        <transactionManager type="JDBC"/>

        <dataSource type="POOLED">
            <property name="driver" value="${driverClassName}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${password}"/>
        </dataSource>
    </environment>
</environments>
<mappers>
    <!--将mapper.xml文件引入-->
    <mapper resource="com/site/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>

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="com.site.mapper.UserMapper">
    <insert id="save" parameterType="user">
        insert into bbs_user(user_username,user_password,user_email,user_gender,user_face,user_phone,
        user_reg_date,user_login_time,user_score,user_category,user_is_activated)
        values (#{username},#{password},#{email},#{gender},#{face},#{phone},#{regTime},#{loginTime},#{score},#{category},#{activated})
    </insert>
    <delete id="remove" parameterType="int">
        delete from bbs_user where user_id = #{id}
    </delete>
    <update id="update" parameterType="user">
        update bbs_user set user_username=#{username},user_password=#{password},user_email=#{email},user_gender=#{gender},user_face=#{face},user_phone=#{phone},
        user_reg_date=#{regTime},user_login_time=#{loginTime},user_score=#{score},user_category=#{category},user_is_activated=#{activated} where user_id=#{id}
    </update>
    <select id="findById" parameterType="int" resultType="user">
        select user_id id,user_username username,user_password password,user_face face,user_email email,
        user_gender gender,user_phone phone,user_reg_date regTime,user_login_time loginTime,user_score score,
        user_is_activated activated,user_category category from bbs_user where user_id=#{id}
    </select>
    <select id="findAll" resultType="user">
        select user_id id,user_username username,user_password password,user_face face,user_email email,
        user_gender gender,user_phone phone,user_reg_date regTime,user_login_time loginTime,user_score score,
        user_is_activated activated,user_category category from bbs_user
    </select>
</mapper>

 

mybatisUtil类
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。
因为只创建一次,所以静态
package com.site.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    private static SqlSessionFactory factory;
    static {
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private MybatisUtil(){}

    public static SqlSessionFactory getSqlSessionFactory() {
        return factory;
    }
}

 


package com.site.mapper;

import com.site.domain.User;

import java.util.List;

public interface UserMapper {
    void save(User user);
    void remove(Integer id);
    void update(User user);
    User findById(Integer id);
    List<User> findAll();
}

user类就不加上了。。。

 

mybatis入门

标签:new   pack   parameter   直接   update   sele   into   tor   build   

原文地址:https://www.cnblogs.com/aboutYouH/p/10033600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!