mybatis基本配置和简单用法

11,导入mybatis依赖

 <!--mydatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>

2,在resources包中配置mybatis.xml文件,用于编写sql语句

<?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="">

   

</mapper>

3,配置连接数据库文件.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="dev">
        <environment id="dev">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/student"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/EmpDao.xml"></mapper>
    </mappers>
</configuration>

4,创建接口,定义方法

public interface StuDao {
    List<Student>getAllStu();

    Student getStuById(int Sno);
    int AddStu(Student student);
    int updateStu(Student student);
    int delStu(int id);

5,创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {
    private int Sno;
    private String Sname;
    private String Ssex;
    private String Sbirthday;
    private int Clazz;

}

6,创建util

public class SqlSessionFactoryUtil {
    //session工厂模式私有化
    private static SqlSessionFactory factory;

    //单例模式,构造私有化
    private SqlSessionFactoryUtil() {
    }

//静态代码块,类加载就运行
    static {
        Reader reader = null;
        try {
            //读取配置文件

       reader= Resources.getResourceAsReader("mybatis.xml");
            factory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (reader!=null){
                    reader.close();
                }

            }catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
    //获得session会话
    public static SqlSession getSqlSession(){
        return factory.openSession(true);//true代表自动提交事务
    }

}

7.1,在编写sql语法实现功能

<?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.by.dao.StuDao">

    <!-- 插入学生信息 -->
    <insert id="AddStu" parameterType="com.by.entity.Student">
        INSERT INTO student (Sname, Ssex, Clazz)
        VALUES (#{Sname}, #{Ssex}, #{Clazz})
    </insert>


    <select id="getAllStu" resultType="com.by.entity.Student">
        select *
        from student
    </select>

    <select id="getStuById" parameterType="int" resultType="com.by.entity.Student">
        select *
        from student
        where Sno = #{Sno}
    </select>

    <update id="updateStu" parameterType="com.by.entity.Student">
        update student
        set Sname=#{Sname},
            Clazz=#{Clazz}
        where Sno=#{Sno}

    </update>

    <delete id="delStu" parameterType="com.by.entity.Student">
        delete from student where Sno=#{dqwoindoqiwdo}
    </delete>



</mapper>

7.2

<?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.by.dao.AdminDao">

    <select id="adminLogin" parameterType="com.by.entity.Admin" resultType="com.by.entity.Admin">
        select *from admin where aname=#{aname} and apwd=#{apwd}
    </select>

</mapper>

8,创建测试类

public class MyTest {
    SqlSession session = null;
    StuDao stuDao = null;
    AdminDao adminDao = null;

    @Before
    public void init() {
        session = SqlSessionFactoryUtil.getSqlSession();

        stuDao = session.getMapper(StuDao.class);

        adminDao = session.getMapper(AdminDao.class);
    }


    //查询
    @Test
    public void testQuery() {


        List<Student> list = stuDao.getAllStu();

        for (Student e : list) {
            System.out.println(e);
        }
    }

    @Test
    //单个查询
    public void testQueryById() {
        Student student = stuDao.getStuById(1);
        System.out.println(student.getSname());
    }

    //增加
    @Test
    public void updateTest() {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();

        StuDao stuDao = session.getMapper(StuDao.class);


        Student student = new Student( );
        student.setSname("小花");
        student.setSsex("女");
        student.setClazz(102);
        stuDao.AddStu(student);
        session.commit();
    }

    @Test
    public void testAdminLogin() {
        Admin admin =new Admin();

        admin.setAname("admin");
        admin.setApwd("123");
        Admin a = adminDao.adminLogin(admin);
        System.out.println(a.getAid()+","+ a.getAname());

    }


    //修改
@Test
    public void updateStu(){
        Student student=stuDao.getStuById(1);
        student.setSname("李文献大傻笔");
        student.setClazz(111);
        stuDao.updateStu(student);
    }

    //删除
    @Test
    public void deleteTest(){
      //  Student student=stuDao.getStuById(1);
        System.out.println(stuDao.delStu(2));
    }

    //销毁
    @After
    public void destory() {
        session.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值