当前位置:   article > 正文

【demo】IntelliJ IDEA + MyBatis + SQL Server_ntellij idea创建java项目,连接sql server数据库 demo

ntellij idea创建java项目,连接sql server数据库 demo

 

【SQL Server】

#1-> 新建数据库【TestDB】

#1->1 新建表【Products】

插入测试数据

#1->2 新建表【UserInfo】

插入测试数据

【IntelliJ IDEA】新建Java项目,项目结构如下

【新建lib文件】添加相应的jar包

loj4j : https://logging.apache.org/log4j/2.x/

mssql-jdbc:https://docs.microsoft.com/zh-cn/sql/connect/jdbc/using-the-jdbc-driver?view=sql-server-2017

mybatishttp://www.mybatis.org/mybatis-3/zh/index.html

配置jar包依赖【File】->【Project Structure】-> 【Modules】->【Dependencies】

选择项目下的【lib】文件夹

【配置DataSource】

【View】->【Tool Windows】->【Database】新建【SQL Server(Microsoft)】链接

填写相应的名称、链接数据库的名字,用户名及密码,默认端口号1433

链接成功后

【JavaBean文件】对应数据库中的每张表结构单独写一个Bean文件,对记录进行封装

  1. package com.mybatisdemo.beans;
  2. public class ProductsBean {
  3. private int ProductID;
  4. private String ProductName;
  5. private double Price;
  6. private String ProductDescription;
  7. public ProductsBean() {
  8. super();
  9. }
  10. public ProductsBean(int productID, String productName, double price, String productDescription) {
  11. ProductID = productID;
  12. ProductName = productName;
  13. Price = price;
  14. ProductDescription = productDescription;
  15. }
  16. public ProductsBean(String productName, double price, String productDescription) {
  17. ProductName = productName;
  18. Price = price;
  19. ProductDescription = productDescription;
  20. }
  21. public int getProductID() {
  22. return ProductID;
  23. }
  24. public void setProductID(int productID) {
  25. ProductID = productID;
  26. }
  27. public String getProductName() {
  28. return ProductName;
  29. }
  30. public void setProductName(String productName) {
  31. ProductName = productName;
  32. }
  33. public double getPrice() {
  34. return Price;
  35. }
  36. public void setPrice(double price) {
  37. Price = price;
  38. }
  39. public String getProductDescription() {
  40. return ProductDescription;
  41. }
  42. public void setProductDescription(String productDescription) {
  43. ProductDescription = productDescription;
  44. }
  45. @Override
  46. public String toString() {
  47. return "ProductsBean{" +
  48. "ProductID=" + ProductID +
  49. ", ProductName='" + ProductName + '\'' +
  50. ", Price=" + Price +
  51. ", ProductDescription='" + ProductDescription + '\'' +
  52. '}';
  53. }
  54. }

  1. package com.mybatisdemo.beans;
  2. import java.util.Date;
  3. public class UserInfoBean {
  4. private int ID;
  5. private String username;
  6. private String birthdate;
  7. private String nationality;
  8. public UserInfoBean() {
  9. super();
  10. }
  11. public UserInfoBean(int id, String username, String birthdate, String nationality) {
  12. this.ID = id;
  13. this.username = username;
  14. this.birthdate = birthdate;
  15. this.nationality = nationality;
  16. }
  17. public UserInfoBean(String username, String birthdate, String nationality) {
  18. this.username = username;
  19. this.birthdate = birthdate;
  20. this.nationality = nationality;
  21. }
  22. public int getID() {
  23. return ID;
  24. }
  25. public void setID(int id) {
  26. this.ID = id;
  27. }
  28. public String getUsername() {
  29. return username;
  30. }
  31. public void setUsername(String username) {
  32. this.username = username;
  33. }
  34. public String getBirthdate() {
  35. return birthdate;
  36. }
  37. public void setBirthdate(String birthdate) {
  38. this.birthdate = birthdate;
  39. }
  40. public String getNationality() {
  41. return nationality;
  42. }
  43. public void setNationality(String nationality) {
  44. this.nationality = nationality;
  45. }
  46. @Override
  47. public String toString() {
  48. return "UserInfoBean{" +
  49. "id=" + ID +
  50. ", username='" + username + '\'' +
  51. ", birthdate=" + birthdate +
  52. ", nationality='" + nationality + '\'' +
  53. '}';
  54. }
  55. }

【Mapper文件】写对应的映射文件,针对每一个表,有对应的接口文件和xml配置文件

【需要注意的地方】

#1-> 接口文件Interface仅仅定义数据库操作函数,不实现具体

#2-> Mapper.xml文件编写的时候需要注意以下几点:

     #2->1 namespace需要对应到相应的接口文件

<mapper namespace="com.mybatisdemo.mapper.ProductsMapper">
<mapper namespace="com.mybatisdemo.mapper.UserInfoMapper">

     #2->2 对应每一个Bean文件对应一个ResultMap,property对应Bean文件中的属性,column对应数据库中的字段

  1. <resultMap id="ProductsDataMap" type="com.mybatisdemo.beans.ProductsBean">
  2. <id property="ProductID" column="ProductID" javaType="java.lang.Integer"></id>
  3. <result property="ProductName" column="ProductName" javaType="java.lang.String"></result>
  4. <result property="Price" column="Price" javaType="java.lang.Double"></result>
  5. <result property="ProductDescription" column="ProductDescription" javaType="java.lang.String"></result>
  6. </resultMap>

    #2->3 insert语句中需要注意ID号自动增长与非自动增长的区别

  1. <insert id="insertData" keyProperty="ProductID">
  2. insert into Products (ProductID, ProductName, Price, ProductDescription)
  3. values (#{ProductID},#{ProductName},#{Price},#{ProductDescription})
  4. </insert>
  1. <insert id="insertData" useGeneratedKeys="true" keyProperty="ID">
  2. insert into UserInfo (username, birthdate, nationality)
  3. values (#{username},#{birthdate},#{nationality})
  4. </insert>

 

  1. package com.mybatisdemo.mapper;
  2. import com.mybatisdemo.beans.ProductsBean;
  3. import java.util.ArrayList;
  4. public interface ProductsMapper {
  5. //新增记录
  6. public int insertData(ProductsBean product) throws Exception;
  7. //更新记录
  8. public int updateData(ProductsBean product) throws Exception;
  9. //删除记录
  10. public void deleteData(int id) throws Exception;
  11. //根据ID查询记录
  12. public ProductsBean selectByID(int id) throws Exception;
  13. //查询所有记录
  14. public ArrayList<ProductsBean> selectAll() throws Exception;
  15. }

注意:ProductsMapper.xml中的id号必须对应接口文件中的函数名称! 

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.mybatisdemo.mapper.ProductsMapper">
  4. <!--自定义返回结果集-->
  5. <resultMap id="ProductsDataMap" type="com.mybatisdemo.beans.ProductsBean">
  6. <id property="ProductID" column="ProductID" javaType="java.lang.Integer"></id>
  7. <result property="ProductName" column="ProductName" javaType="java.lang.String"></result>
  8. <result property="Price" column="Price" javaType="java.lang.Double"></result>
  9. <result property="ProductDescription" column="ProductDescription" javaType="java.lang.String"></result>
  10. </resultMap>
  11. <!--在各种标签中的id属性必须和接口中的方法名相同,id属性值必须唯一的,不能重复使用。
  12. parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型-->
  13. <!--useGeneratedKeys:(仅对insert有用)
  14. 这会告诉MyBatis使用JDBC的getGeneratedKeys方法来取出数据
  15. (比如:像MySQL和SQLServer这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。-->
  16. <!--keyProperty:(仅对 insert有用)
  17. 标记一个属性,MyBatis会通过getGeneratedKeys或者通过insert语句的selectKey子元素设置它的值。默认:不设置。 -->
  18. <!--#{}中的内容,为占位符,当参数为某个JavaBean时,表示放置该Bean对象的属性值-->
  19. <insert id="insertData" keyProperty="ProductID">
  20. insert into Products (ProductID, ProductName, Price, ProductDescription)
  21. values (#{ProductID},#{ProductName},#{Price},#{ProductDescription})
  22. </insert>
  23. <update id="updateData" parameterType="com.mybatisdemo.beans.ProductsBean">
  24. update Products set ProductName = #{ProductName}, Price = #{Price},
  25. ProductDescription = #{ProductDescription} where ProductID = #{ProductID}
  26. </update>
  27. <delete id="deleteData" parameterType="int">
  28. delete from Products where ProductID = #{ProductID}
  29. </delete>
  30. <select id="selectByID" parameterType="int" resultMap="ProductsDataMap">
  31. select * from Products where ProductID = #{ProductID}
  32. </select>
  33. <select id="selectAll" resultMap="ProductsDataMap">
  34. select * from Products
  35. </select>
  36. </mapper>

  1. package com.mybatisdemo.mapper;
  2. import com.mybatisdemo.beans.UserInfoBean;
  3. import java.util.ArrayList;
  4. public interface UserInfoMapper {
  5. //新增记录
  6. public int insertData(UserInfoBean product) throws Exception;
  7. //更新记录
  8. public int updateData(UserInfoBean product) throws Exception;
  9. //删除记录
  10. public void deleteData(int id) throws Exception;
  11. //根据ID查询记录
  12. public UserInfoBean selectByID(int id) throws Exception;
  13. //查询所有记录
  14. public ArrayList<UserInfoBean> selectAll() throws Exception;
  15. }

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.mybatisdemo.mapper.UserInfoMapper">
  4. <!--自定义返回结果集-->
  5. <resultMap id="UserInfoDataMap" type="com.mybatisdemo.beans.UserInfoBean">
  6. <id property="ID" column="ID" javaType="java.lang.Integer"></id>
  7. <result property="username" column="username" javaType="java.lang.String"></result>
  8. <result property="birthdate" column="birthdate" javaType="java.lang.String"></result>
  9. <result property="nationality" column="nationality" javaType="java.lang.String"></result>
  10. </resultMap>
  11. <!--在各种标签中的id属性必须和接口中的方法名相同,id属性值必须唯一的,不能重复使用。
  12. parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型-->
  13. <!--useGeneratedKeys:(仅对insert有用)
  14. 这会告诉MyBatis使用JDBC的getGeneratedKeys方法来取出数据
  15. (比如:像MySQL和SQLServer这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。-->
  16. <!--keyProperty:(仅对 insert有用)
  17. 标记一个属性,MyBatis会通过getGeneratedKeys或者通过insert语句的selectKey子元素设置它的值。默认:不设置。 -->
  18. <!--#{}中的内容,为占位符,当参数为某个JavaBean时,表示放置该Bean对象的属性值-->
  19. <insert id="insertData" useGeneratedKeys="true" keyProperty="ID">
  20. insert into UserInfo (username, birthdate, nationality)
  21. values (#{username},#{birthdate},#{nationality})
  22. </insert>
  23. <update id="updateData" parameterType="com.mybatisdemo.beans.UserInfoBean">
  24. update UserInfo set username = #{username}, birthdate = #{birthdate},
  25. nationality = #{nationality} where ID = #{ID}
  26. </update>
  27. <delete id="deleteData" parameterType="int">
  28. delete from UserInfo where ID = #{ID}
  29. </delete>
  30. <select id="selectByID" parameterType="int" resultMap="UserInfoDataMap">
  31. select * from UserInfo where ID = #{ID}
  32. </select>
  33. <select id="selectAll" resultMap="UserInfoDataMap">
  34. select * from UserInfo
  35. </select>
  36. </mapper>

【mybatisconfg.xml】注册mapper文件

 

  1. jdbc.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
  2. jdbc.url = jdbc:sqlserver://localhost:1433;useUnicode=true;databaseName=TestDB
  3. jdbc.username = sa
  4. jdbc.password = 180710

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <!--引入外部配置文件-->
  5. <properties resource="com/mybatisdemo/confg/sqlserver.properties"></properties>
  6. <!--设置SQL语句打印输出-->
  7. <settings>
  8. <setting name ="logImpl" value="STDOUT_LOGGING"/>
  9. </settings>
  10. <!--配置mybatis运行环境-->
  11. <environments default="mybatis">
  12. <environment id="mybatis">
  13. <!--type="JDBC"代表使用JDBC的提交和回滚来管理事务-->
  14. <transactionManager type="JDBC"></transactionManager>
  15. <!--mybatis提供了3种数据元类型,分别是:POOLEN、UPOOLED、JNDI-->
  16. <!--POOLED表示JDBC数据源连接池-->
  17. <!--UNPOOLED表示不支持数据源连接池-->
  18. <!--JDNI表示外部数据源连接池-->
  19. <dataSource type="POOLED">
  20. <property name="driver" value="${jdbc.driver}"></property>
  21. <property name="url" value="${jdbc.url}"></property>
  22. <property name="username" value="${jdbc.username}"></property>
  23. <property name="password" value="${jdbc.password}"></property>
  24. </dataSource>
  25. </environment>
  26. </environments>
  27. <mappers>
  28. <!-- 告知映射文件方式1,自动扫描包内的Mapper接口与配置文件
  29. <package name="com/mybatis/mapper"/>-->
  30. <!-- 告知映射文件方式2,一个个配置 -->
  31. <mapper resource="com/mybatisdemo/mapper/ProductsMapper.xml"></mapper>
  32. <mapper resource="com/mybatisdemo/mapper/UserInfoMapper.xml"></mapper>
  33. </mappers>
  34. </configuration>

【tools】新建SessionFactory类

  1. package com.demo.tools;
  2. import org.apache.ibatis.io.Resources;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  6. import java.io.Reader;
  7. public class SessionFactory {
  8. public static SqlSessionFactory sessionFactory;
  9. static {
  10. try{
  11. //配置文件地址
  12. String resources = "com/demo/confg/mybatisconfg.xml";
  13. //使用MyBatis提供的Resources类加载mybatis的配置文件
  14. Reader reader = Resources.getResourceAsReader(resources);
  15. //构建sqlSession的工厂
  16. sessionFactory = new SqlSessionFactoryBuilder().build(reader);
  17. System.out.println("静态创建了一次SqlSessionFactory!");
  18. }catch (Exception e){
  19. e.printStackTrace();
  20. }
  21. }
  22. //创建能执行映射文件中sql的sqlSession
  23. public static SqlSession getSession()
  24. {
  25. return sessionFactory.openSession();
  26. }
  27. }

【service】对应bean文件新建数据操作类

需要注意的是:session和mapper的初始化都需要单独写在每个函数内

  1. session = SessionFactory.getSession();
  2. mapper = session.getMapper(ProductsMapper.class);

最开始是放在构造函数里面的,导致程序报错:Executor was closed.

分析原因:因为单独的函数里写了session.close() ,执行下一个函数的时候会发现session已经关闭的情况下又被强迫需要打开执行,导致程序报错!

  1. package com.demo.service;
  2. import com.demo.beans.ProductsBean;
  3. import com.demo.mapper.ProductsMapper;
  4. import com.demo.tools.SessionFactory;
  5. import org.apache.ibatis.session.SqlSession;
  6. import java.util.ArrayList;
  7. public class ProductsDataService {
  8. private SqlSession session;
  9. private ProductsMapper mapper;
  10. public ProductsDataService() {
  11. super();
  12. }
  13. //新增记录
  14. public void insertData(ProductsBean data){
  15. session = SessionFactory.getSession();
  16. mapper = session.getMapper(ProductsMapper.class);
  17. try {
  18. mapper.insertData(data);
  19. System.out.println("[Products] 插入记录成功!");
  20. System.out.println(data.toString());
  21. session.commit();
  22. System.out.println("session commit!");
  23. }catch (Exception e){
  24. e.printStackTrace();
  25. session.rollback();
  26. }finally {
  27. session.close();
  28. System.out.println("session close!");
  29. }
  30. }
  31. //批量插入记录
  32. public void insertMultiData(ArrayList<ProductsBean> listBean){
  33. session = SessionFactory.getSession();
  34. mapper = session.getMapper(ProductsMapper.class);
  35. int count = 0;
  36. try {
  37. count = mapper.insertMultiData(listBean);
  38. session.commit();
  39. System.out.println("session commit!");
  40. }catch (Exception e){
  41. e.printStackTrace();
  42. session.rollback();
  43. }finally {
  44. session.close();
  45. System.out.println("session close!");
  46. }
  47. if(count>0){
  48. System.out.println("[Products] 多行插入记录成功!");
  49. System.out.println("影响数目: " + count);
  50. }
  51. else
  52. System.out.println("插入失败!" + count);
  53. }
  54. //更新记录
  55. public void updateData(ProductsBean data){
  56. session = SessionFactory.getSession();
  57. mapper = session.getMapper(ProductsMapper.class);
  58. try{
  59. mapper.updateData(data);
  60. System.out.println("[Products] 更新记录成功!");
  61. System.out.println(data.toString());
  62. session.commit();
  63. System.out.println("session commit!");
  64. }catch (Exception e){
  65. e.printStackTrace();
  66. session.rollback();
  67. }finally {
  68. session.close();
  69. System.out.println("session close!");
  70. }
  71. }
  72. //删除记录
  73. public void deleteData(int id){
  74. session = SessionFactory.getSession();
  75. mapper = session.getMapper(ProductsMapper.class);
  76. try{
  77. mapper.deleteData(id);
  78. System.out.println("[Products] 已经删除第"+id+"条记录!");
  79. session.commit();
  80. System.out.println("session commit!");
  81. }catch (Exception e){
  82. e.printStackTrace();
  83. session.rollback();
  84. }finally {
  85. session.close();
  86. System.out.println("session close!");
  87. }
  88. }
  89. //根据id查询记录
  90. public void selectDataById(int id){
  91. session = SessionFactory.getSession();
  92. mapper = session.getMapper(ProductsMapper.class);
  93. try{
  94. ProductsBean data = mapper.selectByID(id);
  95. System.out.println(data.toString());
  96. session.commit();
  97. System.out.println("session commit!");
  98. }catch (Exception e){
  99. e.printStackTrace();
  100. session.rollback();
  101. }finally {
  102. session.close();
  103. System.out.println("session close!");
  104. }
  105. }
  106. //查询所有的用户
  107. public void selectAll(){
  108. session = SessionFactory.getSession();
  109. mapper = session.getMapper(ProductsMapper.class);
  110. try{
  111. ArrayList<ProductsBean> listdata = mapper.selectAll();
  112. System.out.println("---------------Products selectAll--------------");
  113. System.out.println("ProductID\t ProductName\t Price\t ProductDescription");
  114. for (ProductsBean data:listdata)
  115. {
  116. // System.out.println(data.toString());
  117. System.out.println(data.getProductID()+"\t "+data.getProductName()+"\t "+data.getPrice()+"\t "+data.getProductDescription());
  118. }
  119. session.commit();
  120. System.out.println("session commit!");
  121. }catch (Exception e){
  122. e.printStackTrace();
  123. session.rollback();
  124. }finally {
  125. session.close();
  126. System.out.println("session close!");
  127. }
  128. }
  129. }

  1. package com.demo.service;
  2. import com.demo.beans.UserInfoBean;
  3. import com.demo.mapper.UserInfoMapper;
  4. import com.demo.tools.SessionFactory;
  5. import org.apache.ibatis.session.SqlSession;
  6. import java.util.ArrayList;
  7. public class UserInfoDataService {
  8. //新增记录
  9. public void insertData(UserInfoBean data){
  10. SqlSession session = SessionFactory.getSession();
  11. UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
  12. try {
  13. mapper.insertData(data);
  14. System.out.println("[UserInfo] 插入记录成功!");
  15. System.out.println(data.toString());
  16. session.commit();
  17. System.out.println("session commit!");
  18. }catch (Exception e){
  19. e.printStackTrace();
  20. session.rollback();
  21. }finally {
  22. session.close();
  23. System.out.println("session close!");
  24. }
  25. }
  26. //更新记录
  27. public void updateData(UserInfoBean data){
  28. SqlSession session = SessionFactory.getSession();
  29. UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
  30. try{
  31. mapper.updateData(data);
  32. System.out.println("[UserInfo] 更新记录成功!");
  33. System.out.println(data.toString());
  34. session.commit();
  35. System.out.println("session commit!");
  36. }catch (Exception e){
  37. e.printStackTrace();
  38. session.rollback();
  39. }finally {
  40. session.close();
  41. System.out.println("session close!");
  42. }
  43. }
  44. //删除记录
  45. public void deleteData(int id){
  46. SqlSession session = SessionFactory.getSession();
  47. UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
  48. try{
  49. mapper.deleteData(id);
  50. System.out.println("[UserInfo] 已经删除第"+id+"条记录!");
  51. session.commit();
  52. System.out.println("session commit!");
  53. }catch (Exception e){
  54. e.printStackTrace();
  55. session.rollback();
  56. }finally {
  57. session.close();
  58. System.out.println("session close!");
  59. }
  60. }
  61. //根据id查询记录
  62. public void selectDataById(int id){
  63. SqlSession session = SessionFactory.getSession();
  64. UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
  65. try{
  66. UserInfoBean data = mapper.selectByID(id);
  67. System.out.println(data.toString());
  68. session.commit();
  69. System.out.println("session commit!");
  70. }catch (Exception e){
  71. e.printStackTrace();
  72. session.rollback();
  73. }finally {
  74. session.close();
  75. System.out.println("session close!");
  76. }
  77. }
  78. //查询所有的用户
  79. public void selectAll(){
  80. SqlSession session = SessionFactory.getSession();
  81. UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
  82. try{
  83. ArrayList<UserInfoBean> listdata = mapper.selectAll();
  84. for (UserInfoBean data:listdata)
  85. {
  86. System.out.println(data.toString());
  87. }
  88. session.commit();
  89. System.out.println("session commit!");
  90. }catch (Exception e){
  91. e.printStackTrace();
  92. session.rollback();
  93. }finally {
  94. session.close();
  95. System.out.println("session close!");
  96. }
  97. }
  98. }

 【测试类】main.java文件

  1. package com.mybatisdemo.main;
  2. import com.mybatisdemo.beans.ProductsBean;
  3. import com.mybatisdemo.beans.UserInfoBean;
  4. import com.mybatisdemo.service.ProductsDataService;
  5. import com.mybatisdemo.service.UserInfoDataService;
  6. public class Main {
  7. public static void main(String[] args) {
  8. // write your code here
  9. //生成一个dataservice类辅助数据操作
  10. ProductsDataService productsDataService = new ProductsDataService();
  11. //查询所有记录
  12. productsDataService.selectAll();
  13. // //插入一行新记录
  14. // ProductsBean product = new ProductsBean(2018,"fuyao",56.76,"同风而起,扶摇直上");
  15. // productsDataService.insertData(product);
  16. //
  17. // productsDataService.selectAll();
  18. //
  19. // //更新记录
  20. // product.setPrice(55.86);
  21. // product.setProductDescription("扶风摇曳,自如纵横");
  22. //
  23. // productsDataService.updateData(product);
  24. //
  25. // productsDataService.selectAll();
  26. //
  27. // //通过id查询记录
  28. // productsDataService.selectDataById(2018);
  29. // //删除记录
  30. // productsDataService.deleteData(2018);
  31. //
  32. // productsDataService.selectAll();
  33. //生成一个dataservice类辅助数据操作
  34. UserInfoDataService userInfoDataService = new UserInfoDataService();
  35. //查询所有记录
  36. userInfoDataService.selectAll();
  37. // //插入一条新记录
  38. // UserInfoBean userInfo = new UserInfoBean("彭于晏","1982-3-24","中国");
  39. // userInfoDataService.insertData(userInfo);
  40. //
  41. // userInfoDataService.selectAll();
  42. //
  43. // //更新记录
  44. // userInfo.setNationality("中国台湾");
  45. // userInfoDataService.updateData(userInfo);
  46. //
  47. // userInfoDataService.selectDataById(8);
  48. // userInfoDataService.selectAll();
  49. //
  50. // //删除记录
  51. // userInfoDataService.deleteData(8);
  52. //
  53. // userInfoDataService.selectAll();
  54. }
  55. }

 【日志log打印】

【src】目录下添加文件,如果不放在/src目录下会报错!

  1. ### 设置Logger输出级别和输出目的地 ###
  2. log4j.rootLogger=debug,stdout,logfile
  3. ### 把日志信息输出到控制台 ###
  4. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  5. #log4j.appender.stdout.Target=System.err
  6. log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
  7. ### 把日志信息输出到文件:jbit.log ###
  8. log4j.appender.logfile=org.apache.log4j.FileAppender
  9. log4j.appender.logfile.File=jbit.log
  10. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
  11. log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
  12. ###显示SQL语句部分
  13. log4j.logger.com.ibatis=DEBUG
  14. log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
  15. log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
  16. log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
  17. log4j.logger.java.sql.Connection=DEBUG
  18. log4j.logger.java.sql.Statement=DEBUG
  19. log4j.logger.java.sql.PreparedStatement=DEBUG

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="OFF">
  3. <Appenders>
  4. <Console name="Console" target="SYSTEM_OUT">
  5. <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
  6. </Console>
  7. </Appenders>
  8. <Loggers>
  9. <Logger name="hive.server2.query.ApiQueryTest" level="trace">
  10. <AppenderRef ref="Console" />
  11. </Logger>
  12. <Logger name="hive.server2.query" level="debug">
  13. <AppenderRef ref="Console" />
  14. </Logger>
  15. <Root level="error">
  16. <AppenderRef ref="Console" />
  17. </Root>
  18. </Loggers>
  19. </Configuration>

【output】

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/559138
推荐阅读
相关标签
  

闽ICP备14008679号