Module: Mybatis::MapperXMLBuilder
- Included in:
- Builder
- Defined in:
- lib/mybatis/builder/mapper_xml_builder.rb
Instance Method Summary collapse
- #build_mapper_xml(workspace, context) ⇒ Object
- #get_all_column(context) ⇒ Object
- #get_class_path(context) ⇒ Object
- #get_mapper_column(field_name) ⇒ Object
- #get_mapper_folder(context) ⇒ Object
- #get_mapper_xml_path(workspace, context) ⇒ Object
- #get_table_name(context) ⇒ Object
- #get_update_values_column(context) ⇒ Object
Instance Method Details
#build_mapper_xml(workspace, context) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 6 def build_mapper_xml(workspace,context) #实体类存放目录 mapper_path = get_mapper_xml_path workspace,context FileUtils.makedirs mapper_path unless File.directory? mapper_path file_path = "#{mapper_path}#{context.po_name}Mapper.xml" #实体类对应文件 file = File.new file_path ,"w" file.puts "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" file.puts "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"" file.puts " \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">" file.puts mapper_folder = "#{self.get_mapper_folder context}" file.puts "<mapper namespace=\"#{mapper_folder}Mapper\">" file.puts " <resultMap id=\"BaseResultMap\" type=\"#{self.get_class_path context}\" >" context.attributes.each_with_index do |attr| file.puts " <result column=\"#{attr.column_name}\" property=\"#{attr.field_name}\" />" end file.puts " </resultMap>" file.puts file.puts " <insert id=\"insert\" parameterType=\"#{self.get_class_path context} \">" file.puts " insert into #{self.get_table_name context} (" file.puts " #{self.get_all_column context}" file.puts ' )' file.puts ' values(' context.attributes.each_with_index do |attr,index| result = self.get_mapper_column attr.field_name if index != context.attributes.size - 1 result << ',' end file.puts " #{result}" end file.puts ' )' file.puts ' </insert>' file.puts file.puts ' <delete id="delete">' file.puts " delete from #{self.get_table_name context} where id = \#{id}" file.puts ' </delete>' file.puts file.puts " <update id=\"update\" parameterType=\"#{self.get_class_path context}\">" file.puts " update #{self.get_table_name context}" file.puts ' set' context.attributes.each_with_index do |attr,index| result = self.get_mapper_column attr.field_name if index != context.attributes.size - 1 result << ',' end file.puts " #{attr.column_name} = #{result}" end file.puts " where id = \#{id}" file.puts ' </update>' file.puts file.puts " <select id=\"select\" resultType=\"#{self.get_class_path context}\" resultMap=\"BaseResultMap\">" file.puts " select * from #{self.get_table_name context} where id = \#{id}" file.puts ' </select>' file.puts '</mapper>' file.close puts "create file: #{file_path}" end |
#get_all_column(context) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 98 def get_all_column(context) result = '' context.attributes.each_with_index do |attr| result << ',' unless result.end_with? ',' result << attr.column_name end result[1,result.size] end |
#get_class_path(context) ⇒ Object
89 90 91 92 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 89 def get_class_path(context) return "#{context.package}.#{context.po_name}" if context.package "#{context.po_name}" end |
#get_mapper_column(field_name) ⇒ Object
107 108 109 110 111 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 107 def get_mapper_column(field_name) result = "\#{" result << "#{field_name}" result << '}' end |
#get_mapper_folder(context) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 68 def get_mapper_folder(context) result = "#{context.po_name}" if context.mapper_package != '' result = "#{context.mapper_package}.#{context.po_name.upcase_first}" if context.mapper_package else result = "#{context.package}.#{context.po_name}" if context.package end result end |
#get_mapper_xml_path(workspace, context) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 78 def get_mapper_xml_path(workspace,context) mapper_path = workspace mapper_path << '/' unless mapper_path.end_with? '/' if context.mapper_package != '' mapper_path << context.mapper_package.gsub(/\./,'/') else mapper_path << context.package.gsub(/\./,'/') if context.package end mapper_path << '/' unless mapper_path.end_with? '/' end |
#get_table_name(context) ⇒ Object
94 95 96 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 94 def get_table_name(context) "t_#{context.po_name.downcase_first.replace_upcase_to_underline}" end |
#get_update_values_column(context) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/mybatis/builder/mapper_xml_builder.rb', line 113 def get_update_values_column(context) result = '' context.attributes.each_with_index do |attr| result << ',\n' if result.end_with? ',\n' result << "#{attr.field_name} = " << '#{' << attr.field_name + '}' end result end |