mybatis链接mysql数据库

mybatis链接mysql数据库

mybatis对数据库的数据的操作

实体类

private int id;
private String name;
private int age;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

dao层的接口
public interface IUserInfoDAO {
/**

  • 查询所有
  • @return
  • @throws Exception
    */
    public List getAll();

/**

  • 新增用户
  • @param user
  • @return
    */
    public int addSession(UserInfo user) throws Exception;

/**

  • 修改
  • @param id
  • @return
    */
    public int updateUser(int id);

/**

  • 删除
  • @param id
  • @return
    */
    public int delUser(int id);

}

xml文件(小配置文件)

这个是小配置文件必不可少的

select * from userinfo insert into userinfo(name,age) value(#{name},#{age}) UPDATE userinfo SET NAME=#{name} WHERE id=#{id} delete from userInfo where id=#{id}

测试类
/**

  • 查询所有
    */
    @org.junit.Test
    public void getAll(){
    SqlSession session = MyBatisUtil.getSession();
    IUserInfoDAO mapper = session.getMapper(IUserInfoDAO.class);
    List all = mapper.getAll();
    for (UserInfo item : all) {
    System.out.println(“编号:”+item.getId()+“姓名:”+item.getName()+“年龄:”+item.getAge());
    }
    session.close();

}

/**

  • 删除
    /
    @org.junit.Test
    public void delUser() throws Exception{
    String resource = “mybatis-config.xml”;
    InputStream inputStream = Resources.getResourceAsStream(resource);
    //获得会话工厂
    SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession session = sf.openSession();
    int count = session.delete(“cn.happy.dao.IUserInfoDAO.delUser”,3);
    System.out.println(count);
    session.commit();
    session.close();
    }
    /
    *

  • 修改
    */
    @org.junit.Test
    public void updateUser() throws Exception{
    String resource = “mybatis-config.xml”;
    InputStream inputStream = Resources.getResourceAsStream(resource);
    //获得会话工厂
    SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

    SqlSession session = sf.openSession();
    UserInfo userInfo=new UserInfo();
    userInfo.setName(“Hello”);
    userInfo.setId(3);
    int count = session.update(“cn.happy.dao.IUserInfoDAO.updateUser”,userInfo);
    System.out.println(count);
    session.commit();
    session.close();
    }

/**

  • 新增

  • @throws Exception
    */
    public void inserts() throws Exception{
    String resource = “mybatis-config.xml”;
    InputStream inputStream = Resources.getResourceAsStream(resource);
    //获得会话工厂
    SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

    SqlSession session = sf.openSession();
    UserInfo userInfo=new UserInfo();
    userInfo.setName(“晓华”);
    userInfo.setAge(25);
    int count = session.insert(“cn.happy.dao.IUserInfoDAO.addSession”,userInfo);
    System.out.println(count);
    session.commit();
    session.close();
    }

大配置文件

        <!-- 事务管理方案 -->
        <!--事务分类:
          编程式事务:  xx.commit() 即需要手动编写
          配置式事务:

          JDBC与MANAGED 区别
        -->
        <transactionManager type="JDBC" />
        <!--
           POOLED:连接池
           UNPOOLED:非连接池
           JNDI:Java Naming ANd Directory Interface  java命名和目录接口
        -->
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="username" value="root" />
            <property name="password" value="123156" />
        </dataSource>
    </environment>
    <!--该内容在一个xml中可以有多个-->
    <!--<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/test"/>
            <property name="username" value="root" />
            <property name="password" value="123156" />
        </dataSource>
    </environment>-->
</environments>
<!--映射文件:描述某个实体和数据库表的对应关系 -->
<mappers>
   <mapper resource="cn/happy/dao/IUserInfoDAO.xml" />
  </mappers>

即便有了上面的一系列配置,但是也不法访问到数据库
必须要有数据的依赖

log4j log4j 1.2.17 mysql mysql-connector-java 5.1.39
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页